I have the following hierarchy:
class base
{
public:
virtual ~base(){}
virtual void foo() {}
};
template <typename T>
class derived1 : public base
{
virtual void foo() {};
};
template <typename T>
class derived2 : public base
{
virtual void foo() {};
};
Now given a pointer to base, I'd like to find out if the underlying is either derived1 or derived2. The problem is that both derived1 and derived2 can be specialised on many different types, using dynamic_cast to test for a down cast requires the template type to be know. I've ended up with messy, unmaintable and incomplete bit of code:
base* b = new derived1<int>();
if (dynamic_cast<derived1<int>*> ||
dynamic_cast<derived1<unsigned int>*> ||
dynamic_cast<derived1<double>*>)
std::cout << "is derived1";
else if (dynamic_cast<derived2<int>*> ||
dynamic_cast<derived2<unsigned int>*> ||
dynamic_cast<derived2<double>*>)
std::cout << "is derived2";
Is there a better way, that can handle any type specialization?
Move the logic which depends on the type into the type.
Instead of:
if (dynamic_cast<derived1<int>*>(b) ||
dynamic_cast<derived1<unsigned int>*>(b) ||
dynamic_cast<derived1<double>*>(b))
std::cout << "is derived1";
else if (dynamic_cast<derived2<int>*>(b) ||
dynamic_cast<derived2<unsigned int>*>(b) ||
dynamic_cast<derived2<double>*>(b))
std::cout << "is derived2";
add a virtual print_name() const
function to base
, and then do:
void example() {
std::unique_ptr<base> b(new derived1<int>());
b->print_name();
}
class base
{
public:
~base(){}
virtual void foo() {}
virtual void print_name() const = 0;
};
template <typename T>
class derived1 : public base
{
virtual void foo() {}
virtual void print_name() const {
std::cout << "is derived1";
}
};
template <typename T>
class derived2 : public base
{
virtual void foo() {}
virtual void print_name() const {
std::cout << "is derived2";
}
};
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