Base to derived cast

Heya, I'm just curious. Is there such a cast, that a base class (pointer) could be casted to a derived class (pointer), in which the base data members are maintained and the derived are declared via the default constructor? If not, how would this be possible with basic templating?
Last edited on
You cannot change an object's type. Once you create an object of a specific type, it be that type until it is destroyed.


The only way to do what you're suggesting would be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
Base* p = new Base;

// now we want to convert that to a 'Derived'
// we can't turn the above object into a Derived.  The best we can 
//  do is create a new object:

Derived* d = new Derived( *p );

// and reassign:

delete p;
p = d;
Topic archived. No new replies allowed.