Storing downcast objects in a vector

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
 
K

Karthik Kumar

Chester said:
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

How is Blist defined ?
Blist.push_back( new B() );
Blist.push_back( new B() );

A *a = new A();

Ok - You create a pointer to an object of type A.

B *b = static_cast<B*>(a);

Did u really want static_cast here / or did
you mean dynamic_cast. The compiler
would just obey you. But b is not pointing to an
object of type B , since you have not created one till now.

B->setProperty(...);

Did you mean b->setProperty here ?

Even if that is the case, you are getting into
*UB* - undefined behaviour - since you have forced
the compiler to make an invalid cast before.
You have not allocated an object of type B before in your
code. How can you make this access anyway.

Blist.push_back( new B() ); <- throws heap exception

Did you mean -
Blist.push_back( b ); ??

Please post compilable code-fragment .
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?

May be you want dynamic_cast that would help your problems.
And just to add,

A * a = new A;
B * b = dynamic_cast<B *>(a) ; // would fail fail
// i.e. b would be 0 .
// you can use assert to check it .

A * a = new B;
B * b = dynamic_cast<B *>(a) ; // ok - runtime would be happy.
 
R

Rob Williscroft

Chester wrote in in
comp.lang.c++:
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);

This is Undefined Behaviour(tm), it may work on your implementation
but then again it may cause *anything* to happen (as far as Standard C++
is concerned).
B->setProperty(...);

Did you mean b->stProperty(), anyway more UB on top of UB, anything
can happen.
Blist.push_back( new B() ); <- throws heap exception

What is Blist, is it std::vector< B * >, if so I don't see why
you get a heap exception here, other than you've already violated
Standard C++ rules and have UB anyway (but see below too).
There is only one memory allocation which is when creating a.

Well std::vector does memory allocation and object construction
and copying when you call push_back().
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.

When you call b->setProprty(), you treat an A object as if it was a
B object probably corrupting the heap. This is just UB manifesting
itself.
Is there something wrong with this design? Is it impossible to store a
downcast obj after setting its members in the derived class?

You should only "downcast" an object that is genuine object of the
derived type, i.e. one that is the result of a prior "upcast".

struct A {};
struct B : A {};

void f( A *a, bool its_really_a_B )
{
if ( its_really_a_B )
{
B *b = static_cast< B * >( a );
// can now use a as a B*.
}
}

int main()
{
A a;
B b;
f( &a, false );
f( &b, true );
}

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top