How Does compiler implement virtual destructor ???

Discussion in 'C++' started by Juha Nieminen, Apr 16, 2008.

  1. Pallav singh wrote:
    > Hi All
    >
    > How Does compiler implement virtual destructor ???
    >
    > as we all know if base class destructor is virtual..........then while
    > wriiting statemnt like this
    >
    > Base * b = new Derived( );
    > delete b;
    >
    > // it will call Deived class destructor Followed by Base Class
    >
    > How compiler does House-keeping for it in V-Table ??


    It's most probably again one thing which the standard doesn't specify,
    but which all compilers do in the same way:

    If there exists any virtual functions in a class (eg. a virtual
    destructor) the size of the class will be increased by a pointer.
    Internally this pointer points to a virtual table which contains
    pointers to the virtual functions at fixed offsets. When calling a
    virtual function (eg. the destructor), what it actually does is that it
    takes that pointer, indexes it with a fixed value, takes the function
    pointer found there and calls that function. If it's a destructor, then
    the destructor will call the destructor of its own base class, etc.
    Juha Nieminen, Apr 16, 2008
    #1
    1. Advertising

  2. Juha Nieminen

    Pallav singh Guest

    Hi All

    How Does compiler implement virtual destructor ???

    as we all know if base class destructor is virtual..........then while
    wriiting statemnt like this

    Base * b = new Derived( );
    delete b;

    // it will call Deived class destructor Followed by Base Class

    How compiler does House-keeping for it in V-Table ??

    Thanks
    Pallav Singh
    Pallav singh, Apr 16, 2008
    #2
    1. Advertising

  3. Pallav singh a écrit :
    > How Does compiler implement virtual destructor ???
    >
    > as we all know if base class destructor is virtual..........then while
    > wriiting statemnt like this
    >
    > Base * b = new Derived( );
    > delete b;
    >
    > // it will call Deived class destructor Followed by Base Class
    >
    > How compiler does House-keeping for it in V-Table ??


    The destructor of the derived class will be called like any other
    virtual function, that is why if you don't make it virtual, you will not
    destroy the most derived object.

    When delete calls:
    b->~Base();

    it will call:
    b->~Derived();

    Michael
    Michael DOUBEZ, Apr 16, 2008
    #3
  4. Juha Nieminen

    raof01 Guest

    Hello Pallav,

    > How Does compiler implement virtual destructor ???
    > as we all know if base class destructor is virtual..........then while
    > wriiting statemnt like this
    > Base * b = new Derived( );
    > delete b;
    > // it will call Deived class destructor Followed by Base Class How
    > compiler does House-keeping for it in V-Table ??
    >


    Compiler does the same to virtual destructor as it does to other virtual
    functions.

    In destructor (maybe), compiler inserts some code to free the space hold
    by VTABLE.

    raof01
    mailto:
    "Thoughts are but dreams till their effects be tried." -- William Shakespeare
    raof01, Apr 16, 2008
    #4
  5. Juha Nieminen

    James Kanze Guest

    Re: How Does compiler implement virtual destructor ???

    On Apr 16, 10:08 am, Michael DOUBEZ <> wrote:
    > Pallav singh a écrit :


    > > How Does compiler implement virtual destructor ???


    > > as we all know if base class destructor is
    > > virtual..........then while wriiting statemnt like this


    > > Base * b = new Derived( );
    > > delete b;


    > > // it will call Deived class destructor Followed by Base Class


    > > How compiler does House-keeping for it in V-Table ??


    > The destructor of the derived class will be called like any
    > other virtual function, that is why if you don't make it
    > virtual, you will not destroy the most derived object.


    If you don't make it virtual, you'll have undefined behavior.
    It might not destruct the most derived object, it might not free
    the memory at all, it might crash, or it might cause a subtle
    bug to appear later in your code.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 16, 2008
    #5
  6. raof01 wrote:
    > Compiler does the same to virtual destructor as it does to other virtual
    > functions.


    Not *exactly* the same. In the case of virtual functions only the
    function of the most derived class (of the type of the object) is
    called. In the case of destructors, *all* destructors are called in the
    inheritance hierarchy, not just one.
    Juha Nieminen, Apr 16, 2008
    #6
    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. Calvin Lai
    Replies:
    7
    Views:
    551
    Calvin Lai
    Dec 18, 2003
  2. Chunhui Han
    Replies:
    2
    Views:
    498
  3. qazmlp
    Replies:
    1
    Views:
    564
    qazmlp
    Apr 10, 2005
  4. Replies:
    2
    Views:
    452
    Victor Bazarov
    Dec 16, 2005
  5. arun
    Replies:
    2
    Views:
    540
    benben
    Jun 13, 2006
Loading...

Share This Page