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.
This answer pertains to the question as it was at the time of this answer.
Don't use CRTP, which is not dynamic polymorphism, to create dynamic polymorphism.
Use a virtual function.
That's what they're for.
class Base
{
private:
virtual
auto implementation() -> double = 0;
public:
auto interface() -> double { return implementation(); }
};
class DerivedA
: public Base
{
private:
auto implementation() -> double override { return 2.0; }
};
class DerivedB
: public Base
{
private:
auto implementation() -> double override { return 1.0; }
};
Alf's suggestion is on target. It is easy to adapt it to your additional requirement. Define an interface with a pure virtual method:
struct BaseInterface {
virtual ~BaseInterface() {}
virtual double interface() = 0;
};
Now, your template base class can derive from the interface:
template <typename Derived>
class Base : BaseInterface {
public:
double interface(){
static_cast<Derived*>(this)->implementation();
}
};
Now, you can create a vector of pointers to the interface:
std::vector<std::shared_ptr<BaseInterface>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With