C2664 when casting child class to templated parent class

Posted by DC on Stack Overflow See other posts from Stack Overflow or by DC
Published on 2010-04-16T18:15:01Z Indexed on 2010/04/16 18:23 UTC
Read the original article Hit count: 271

Filed under:
|
|
|
|

I have a parent class which is templated, and a child class which implements it.

template< typename T1, typename T2>
class ParentClass{ . . . };

class ChildClass : public ParentClass<MyT1, MyT2> { . . . };

And I want to have a pointer which I can use polymorphically:

ParentClass<T1, T2>* ptr;
ptr = static_cast<ParentClass<MyT1, MyT2>* >(new ChildClass() );

No matter how I cast it, I always get a C2664 which has the same expression:

error C2664: cannot convert parameter 1 from 'ParentClass< T1,T2> *' to 'ParentClass< T1,T2> *'

Is it not possible to cast pointer types between inherited types if the parent is templated, even if the types specified in the templates are the same?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates