I have the following hierarchy:
class base
{
public:
virtual ~base(){}
virtual void foo() {}
};
template
class derived1 : public base
{
Insert a non-templated class inbetween base
and derived1
or derived2
:
class base
{
public:
virtual ~base() {} // **NOTE** Should be virtual
virtual void foo() {}
};
class derived1_base : public base
{
};
template
class derived1 : public derived1_base
{
public:
virtual void foo() {}
};
class derived2_base : public base
{
};
template
class derived2 : public derived2_base
{
public:
virtual void foo() {}
};
In a comment, you mentioned:
[I want to] call a particular function for each one - btw there's more than derived1 and derived2
Add that (virtual) function to derived1_base
, and you don't even need to know T
anymore.
if (dynamic_cast(foo))
{
std::cout << "is derived1";
dynamic_cast(foo)->specific_derived1_function();
}
else if (dynamic_cast(foo))
{
std::cout << "is derived2";
dynamic_cast(foo)->specific_derived2_function();
}
NOTE: I consider a list of dynamic_cast<>
a code smell, and I urge you to rethink your approach.