473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Is it necessary (design-wise) to define an abstract class as template's argument?

Here is my question:
I want to have a class, which requires a typename T with interface
"operate()" and "Init()" as its parameter.

template <class Tclass Foo {
public:
Foo(int param) { _algorithm.Init(param); }
void do_algorithm(vector<inta) {
_algorithm.operate(a);
....
}
private:
T _algorithm;
};

I understand this is not necessary to define an interface class (for
example, as long as DumbAlgrithm provide functions that Foo needs,
it's good). But I am thinking if having a class hierarchy helps code
maintenance. What's the normal approach in this case? Thanks a lot.

class AbstractAlgorithm {
public:
virtual void operate(vector<inta) = 0;
virtual void Init(int param) = 0;
};

Then any class that actually used as parameter by Foo will be the sub
class of AbstractAlgorithm, e.g.,
class DumbAlgorithm : public AbstractAlgorithm {
public:
virtual void operate(vector<inta) {}
virtual void Init(int param) {}
};

Aug 16 '07 #1
1 1468
On 16 Srp, 02:48, newbie <mitbb...@yahoo.comwrote:
Here is my question:
I want to have a class, which requires a typename T with interface
"operate()" and "Init()" as its parameter.

template <class Tclass Foo {
public:
Foo(int param) { _algorithm.Init(param); }
void do_algorithm(vector<inta) {
_algorithm.operate(a);
....
}
private:
T _algorithm;

};

I understand this is not necessary to define an interface class (for
example, as long as DumbAlgrithm provide functions that Foo needs,
it's good). But I am thinking if having a class hierarchy helps code
maintenance. What's the normal approach in this case? Thanks a lot.

class AbstractAlgorithm {
public:
virtual void operate(vector<inta) = 0;
virtual void Init(int param) = 0;

};

Then any class that actually used as parameter by Foo will be the sub
class of AbstractAlgorithm, e.g.,
class DumbAlgorithm : public AbstractAlgorithm {
public:
virtual void operate(vector<inta) {}
virtual void Init(int param) {}

};
If you have common abstract class, you do not need to use template for
such issue. You can simply pass some instance (or reference to
instance or pointer to instance) of AbstractAlgorithm to constructor
of Foo and then use it.

Aug 16 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
7
by: ankitjain.bvcoe | last post by:
Hi i have the following problem in my design :::: i want to define an abstract class LogBuffer and derive two singleton classes from it i.e AlarmBuffer and FireWallBuffer.For this my design is...
2
by: tony | last post by:
Hello!! I know what an abstract class is which mean that the one of the derived class must define the abstract methods in the abstract class. So all the abstract methods in the abstarct class...
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
2
by: talkingpidgin | last post by:
I am trying to figure out why it is not conventional to use protected constructors in abstract classes since the only time they should be called is by the constructors of it's derived classes. Is...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
4
by: Pranav | last post by:
Hello All, I have a simple question regarding the definition of abstract class, IIRC , Abstract class is one which contains virtual function declaration and other variables and no object of this...
4
by: dascandy | last post by:
Hi, For a project I'm working on I'm kind-of-hacking my way around deriving a class from an interface or such to create a mock, but instead creating the mock directly. It is usable as the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.