N
newbie
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 T> class Foo {
public:
Foo(int param) { _algorithm.Init(param); }
void do_algorithm(vector<int> a) {
_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<int> a) = 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<int> a) {}
virtual void Init(int param) {}
};
I want to have a class, which requires a typename T with interface
"operate()" and "Init()" as its parameter.
template <class T> class Foo {
public:
Foo(int param) { _algorithm.Init(param); }
void do_algorithm(vector<int> a) {
_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<int> a) = 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<int> a) {}
virtual void Init(int param) {}
};