If that's all you need to do, then just don't discard type information until you absolutely must:
A<T>* a = new A<T>;
a->pass_A(new NODE);
POLY_TYPE = a;
Edit:
You didn't post any error message on your update - funny how so many people who ask questions here believe that the actual error is irrelevant - but you also have a type problem.
Since you pass a NODE*
to pass_A, the template parameter must be NODE*
, not NODE
.
The following compiles (cleanup of the version with no virtuals, with my tweak):
class NODE
{
public:
NODE(){}
~NODE(){}
NODE& operator=(const NODE&) { return *this; }
};
template <class T>
class A;
template <class T>
class BASE
{
public:
BASE(){}
BASE(int mode)
{
if(mode == 1)
{
init_BASE();
}
else if(mode == 0)
{
init_A();
}
}
private:
void init_BASE()
{
POLY_TYPE = new BASE<T>;
}
void init_A()
{
A<T>* a = new A<T>;
a->pass_A(new NODE);
POLY_TYPE = a;
}
BASE* POLY_TYPE;
};
template <class T>
class A : public BASE<T>
{
public:
void pass_A(T DATA)
{
this->DATA = DATA;
}
private:
T DATA;
};
int main()
{
BASE<NODE*> base(1);
return 0;
}