On May 7, 11:32*pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> > * * void f()
> > * * {
> > * * * * void* p = malloc(sizeof(D));
> > * * * * new (p) D; // ...placement new
> > * * * * B* b = (B*) p;
>
> That's *not* a good idea. *You should do
>
> * * * D* pd = new (p) D; // ...placement new
> * * * B* b = pd;
>
> instead.
>
> > * * * * b->~B(); // ...is virtual destructor in effect?
> > * * * * free(p);
> > * * }
>
> > When f() is executed will D::~D() be called?
>
> With your current cast from 'p' to a B*, the behaviour is undefined I'm
> afraid.
(1) Can you tell me what the difference is between:
new (p) D;
B* b = (B*) p;
and
D* pd = new (p) D;
B* b = pd;
I would have thought they were equivalent? Why aren't they?
(2) If I use your recommended form, will the virtual destructor of
D::~D be called if I call b->~B() ?
Thanks,
Andrew.
|