I've got a class Base
from which I have two classes, DerivedA
and DerivedB
as defined below.
template <typename Derived>
class Base{
public:
double interface(){
static_cast<Derived*>(this)->implementation();
}
};
class DerivedA : public Base<DerivedA>{
public:
double implementation(){ return 2.0;}
};
class DerivedB : public Base<DerivedB>{
public:
double implementation(){ return 1.0;}
};
In short, I'm trying to do the following to maintain a collection of objects, some of which are DerivedA
and some of which are DerivedB
:
std::vector<std::shared_ptr<Derived>>
Which is obviously impossible beacuse I've now made the class Derived
a templated class.
Is there any way I can create / maintain a polymorphic collection of objects?
EDIT: Unfortunately, a simple templated structure does not work as the function implementation
is templated in my actual program -- so then implementation
would have to be a templated pure virtual function, which cannot be. Pardon my lack of explanation.