C
Chester
Is it possible to new an object A, downcast it to B, and store B in a
vector?
I'm having problem with the following code:
B is a derived class of A
Blist.push_back( new B() );
Blist.push_back( new B() );
A *a = new A();
B *b = static_cast<B*>(a);
B->setProperty(...);
Blist.push_back( new B() ); <- throws heap exception
There is only one memory allocation which is when creating a.
B->setProperty only sets primitive variables. But everytime when I
have B->setProperty enable, the program will crash on the third
push_back. In this example, I'm not even storing b in the list but a
new B.
Is there something wrong with this design? Is it impossible to store a
downcast obj after setting its members in the derived class?
Thanks,
Chester
vector?
I'm having problem with the following code:
B is a derived class of A
Blist.push_back( new B() );
Blist.push_back( new B() );
A *a = new A();
B *b = static_cast<B*>(a);
B->setProperty(...);
Blist.push_back( new B() ); <- throws heap exception
There is only one memory allocation which is when creating a.
B->setProperty only sets primitive variables. But everytime when I
have B->setProperty enable, the program will crash on the third
push_back. In this example, I'm not even storing b in the list but a
new B.
Is there something wrong with this design? Is it impossible to store a
downcast obj after setting its members in the derived class?
Thanks,
Chester