what happens internally

Discussion in 'C++' started by swesoc, Dec 13, 2005.

  1. swesoc

    swesoc Guest

    Hello Friend,

    I know this question is been asked many times,

    if a have base pointer pointing to derived object & if i delete that
    pointer , if the base class destructor is not virtual. then what
    happens internally ?

    every 1 it gives an infinite behaviour, does anybody know what happens
    exactly
    swesoc, Dec 13, 2005
    #1
    1. Advertising

  2. swesoc wrote:
    > Hello Friend,
    >
    > I know this question is been asked many times,
    >
    > if a have base pointer pointing to derived object & if i delete that
    > pointer , if the base class destructor is not virtual. then what
    > happens internally ?


    C.a.t.a.s.t.r.o.p.h.e

    >
    > every 1 it gives an infinite behaviour, does anybody know what happens
    > exactly


    Its not "infinite behaviour", it is "undefined behaviour". And by
    undefined behaviour, we really mean u.n.d.e.f.i.n.e.d behaviour.
    Anything may happen. Compiler is free to send mails to all people in
    your address book.

    On a more serious note, the compiler will not be able to understand
    that the pointer points to an object of derived class. It might simply
    destroy the base class portion, and not the derived class portion. This
    will result in memory leaks, incomplete destruction of object, and what
    not, collectively called as undefined behaviour.
    Neelesh Bodas, Dec 13, 2005
    #2
    1. Advertising

  3. swesoc

    Axter Guest

    swesoc wrote:
    > Hello Friend,
    >
    > I know this question is been asked many times,
    >
    > if a have base pointer pointing to derived object & if i delete that
    > pointer , if the base class destructor is not virtual. then what
    > happens internally ?
    >
    > every 1 it gives an infinite behaviour, does anybody know what happens
    > exactly


    (undefined) behaviour means anything can happen.

    If you have a requirement to keep the base destructor non-virtual, you
    can consider using the following smart pointer, which will correctly
    call the derived class destructor (If the derived class type is passed
    to the constructor).
    http://code.axter.com/copy_ptr.h
    Also cow poitner:
    http://code.axter.com/cow_ptr.h

    But I'm not advocating doing this unless you have a really good reason
    for keeping the base class destructor non-virtual.
    Axter, Dec 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saifee

    send email internally

    Saifee, Dec 8, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    474
  2. Jon Maz
    Replies:
    2
    Views:
    464
    Jon Maz
    Sep 30, 2004
  3. =?Utf-8?B?TWlrZQ==?=

    Is ASP.NET App Being Accessed Internally/Externally

    =?Utf-8?B?TWlrZQ==?=, Feb 5, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    382
    =?Utf-8?B?TWlrZQ==?=
    Feb 5, 2007
  4. How function works internally

    , Sep 27, 2006, in forum: C Programming
    Replies:
    15
    Views:
    1,575
    Michael Wojcik
    Oct 3, 2006
  5. NM
    Replies:
    6
    Views:
    463
    Default User
    Sep 20, 2006
Loading...

Share This Page