Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual Destructor

Reply
Thread Tools

Virtual Destructor

 
 
Ron Natalie
Guest
Posts: n/a
 
      11-07-2003

"Dan Cernat" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...

>
> I tried your suggestion and although I got the behaviour one would
> expect (destruct of derived objects) I have to agree that it could
> lead to undefined behaviour, especially if the pointer is passed
> around and one tries to access its members (not at the index 0).
> However, the memory manager seems that is doing the right job.


Not lead to undefined behavior, it is undefined behavior. Coincidentally
working is one of those insidious undefined behaviors. Try putting
some more data members in. It will be sure to break. The issue is not
just delete, you can ONLY convert the derived pointer of one object to
a base class. It doesn't work in arrays.

Image sizeof(base) is 4. sizeof(derived) is 8.
What is the relationship the base_array[1] with respect to base_array[0].
What is the relationship of derived_array[1] to derived_array[0].
Just think about it for a minute.


 
Reply With Quote
 
 
 
 
Dan Cernat
Guest
Posts: n/a
 
      11-10-2003

"Ron Natalie" <(E-Mail Removed)> wrote in message
news:3fabdedc$0$3632$(E-Mail Removed) ...
>
> "Dan Cernat" <(E-Mail Removed)> wrote in message

news:(E-Mail Removed) om...
>
> >
> > I tried your suggestion and although I got the behaviour one would
> > expect (destruct of derived objects) I have to agree that it could
> > lead to undefined behaviour, especially if the pointer is passed
> > around and one tries to access its members (not at the index 0).
> > However, the memory manager seems that is doing the right job.

>
> Not lead to undefined behavior, it is undefined behavior. Coincidentally
> working is one of those insidious undefined behaviors. Try putting
> some more data members in. It will be sure to break. The issue is not
> just delete, you can ONLY convert the derived pointer of one object to
> a base class. It doesn't work in arrays.
>
> Image sizeof(base) is 4. sizeof(derived) is 8.
> What is the relationship the base_array[1] with respect to base_array[0].
> What is the relationship of derived_array[1] to derived_array[0].
> Just think about it for a minute.
>

Yup, this is what I wanted to say in my prevoius message and I totally agree
with what you say.
I really should think twice before posting. Point taken.

Thanks,
Dan


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin C++ 12 04-07-2006 09:02 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM
Virtual destructor for virtual base class? Chunhui Han C++ 2 06-24-2004 10:13 AM
virtual destructor Vs virtual method Calvin Lai C++ 7 12-18-2003 03:11 AM



Advertisments