Here is a sample code:
class Base {
public:
virtual void common();
};
class Derived {
public:
void common();
virtual void spec(); // added function specific for this class
};
class BaseTracker {
public:
void add(Base* p);
private:
vector < Base* > vec;
};
class DerivedTracker {
public:
void add(Derived* p);
private:
vector < Derived* > vec;
};
I want DerivedTracker and BaseTracker to be derived from class Tracker, because a lot of code for these two classes is the same, except one method, add(). DerivedTracker::add() method needs to call functions specific to Derived class. But I don’t want to do dynamic casting. I think it is not the case when I should use it. Also Tracker class should include container, so functions which are implemented in this class could use it.
It sounds like the Tracker class would best be a template instead of being derived from a common ancestor:
You could then add a specialization of the
add()method that usesDerived‘s special features: