#include
using namespace std;
class Base {
public:
Base() {};
~Base() {};
};
template
class Derived: public Base {
T _val;
public
You must change the base type to be polymorphic:
class Base {
public:
Base() {};
virtual ~Base(){};
};
To cast from some supertype to some derived type, you should use dynamic_cast
:
Base *b = new Derived(1);
Derived *d = dynamic_cast *>(b);
Using dynamic_cast
here checks that the typecast is possible. If there is no need to do that check (because the cast cannot fail), you can also use static_cast
:
Base *b = new Derived(1);
Derived *d = static_cast *>(b);