Calling the vitual functions in the base class destructor

Discussion in 'C++' started by doublemaster007@gmail.com, Feb 7, 2009.

  1. Guest

    1) Can we call pure virtual functions from base class destructor?
    2) Can we call virtual functions from base class destructor? which
    function will be invoked?

    What i think is either of them ends with crashing. because when
    destructing the base class destructor, derived subobject is already
    destroyed! But i have read some where that (1) ends with crash, second
    one is not.
     
    , Feb 7, 2009
    #1
    1. Advertising

  2. Bo Persson Guest

    wrote:
    > 1) Can we call pure virtual functions from base class destructor?
    > 2) Can we call virtual functions from base class destructor? which
    > function will be invoked?
    >
    > What i think is either of them ends with crashing. because when
    > destructing the base class destructor, derived subobject is already
    > destroyed! But i have read some where that (1) ends with crash,
    > second one is not.


    When you are in a constructor or a destructor, the object is always of
    exactly the type of the class containing these special members. Any
    derived parts have not been constructed yet, or already been
    destroyed.

    Calling a virtual function works just as it would for an object of the
    exact class. The same for pure virtual functions. Whether that
    "crashes" or not, is undefined.



    Bo Persson
     
    Bo Persson, Feb 7, 2009
    #2
    1. Advertising

  3. SG Guest

    On 7 Feb., 12:27, ""
    <> wrote:
    > 1) Can we call pure virtual functions from base class destructor?


    No.

    > 2) Can we call virtual functions from base class destructor? which
    > function will be invoked?


    Destructors are called in reverse order of constructors. Constructors
    are invoked from base class to derived class. After the constructor is
    finished, the object IS an instance of the class. After the destructor
    is finished the object IS NOT an instance of the class ANYMORE.

    If you call a virtual function in a destructor X::~X it will invoke
    the function of class X if X overrides the virtual function or the
    function of some other base class.

    Cheers!
    SG
     
    SG, Feb 7, 2009
    #3
  4. terminator Guest

    On Feb 7, 2:27 pm, ""
    <> wrote:
    > 1) Can we call pure virtual functions from base class destructor?
    > 2) Can we call virtual functions from base class destructor? which
    > function will be invoked?
    >
    > What i think is either of them ends with crashing. because when
    > destructing the base class destructor, derived subobject is already
    > destroyed! But i have read some where that (1) ends with crash, second
    > one is not.


    It seems that case 1 is likely to happen.If the compiler does not
    catch this error (first senario),a runtime or rational error is
    expected .I call the later two senarios a **crash**.
    I do not not what standard says about this case.

    regards,
    FM.
     
    terminator, Feb 7, 2009
    #4
  5. terminator Guest

    On Feb 7, 2:27 pm, ""
    <> wrote:
    > 1) Can we call pure virtual functions from base class destructor?
    > 2) Can we call virtual functions from base class destructor? which
    > function will be invoked?
    >
    > What i think is either of them ends with crashing. because when
    > destructing the base class destructor, derived subobject is already
    > destroyed! But i have read some where that (1) ends with crash, second
    > one is not.


    PS:it is also possible that a null function exeption is automatically
    neglected .

    regards,
    FM.
     
    terminator, Feb 7, 2009
    #5
  6. Guest

    On Feb 7, 9:40 pm, terminator <> wrote:
    > On Feb 7, 2:27 pm, ""
    >
    > <> wrote:
    > > 1) Can we call pure virtual functions from base class destructor?
    > > 2) Can we call virtual functions from base class destructor? which
    > > function will be invoked?

    >
    > > What i think is either of them ends with crashing. because when
    > > destructing the base class destructor, derived subobject is already
    > > destroyed! But i have read some where that (1) ends with crash, second
    > > one is not.

    >
    > PS:it is also possible that a null function exeption is automatically
    > neglected .
    >
    > regards,
    > FM.


    Hmm..if we call virtual functions from base class functions it would
    call dervied class implementation

    but in i call virtual functions from base class destructor it would
    call base class functions? Why the compilers dont behave as normaly
    user predicts? I think most of the people would think otherwise in
    this case i guess..
     
    , Feb 8, 2009
    #6
  7. James Kanze Guest

    On Feb 7, 5:36 pm, terminator <> wrote:
    > On Feb 7, 2:27 pm, ""


    > <> wrote:
    > > 1) Can we call pure virtual functions from base class destructor?
    > > 2) Can we call virtual functions from base class destructor? which
    > > function will be invoked?


    > > What i think is either of them ends with crashing. because
    > > when destructing the base class destructor, derived
    > > subobject is already destroyed! But i have read some where
    > > that (1) ends with crash, second one is not.


    > It seems that case 1 is likely to happen.If the compiler does
    > not catch this error (first senario),a runtime or rational
    > error is expected .I call the later two senarios a **crash**.
    > I do not not what standard says about this case.


    If the call is through an unqualified name (i.e. just f(), and
    not C::f()), and the function is virtual, dynamic lookup is
    used. If dynamic lookup resolves to a pure virtual function,
    the behavior is undefined (although most compilers will cause
    the program to crash).

    In the destructor, the dynamic type of the object is the type of
    the destructor. So if the function is declared pure virtual in
    that class, calling it with an unqualified name is undefined
    behavior. You can still call it with a qualified name, however.
    (In that case, of coures, you have to provide an
    implementation.)

    --
    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, Feb 8, 2009
    #7
  8. James Kanze Guest

    On Feb 7, 5:40 pm, terminator <> wrote:
    > On Feb 7, 2:27 pm, ""


    > <> wrote:
    > > 1) Can we call pure virtual functions from base class destructor?
    > > 2) Can we call virtual functions from base class destructor? which
    > > function will be invoked?


    > > What i think is either of them ends with crashing. because
    > > when destructing the base class destructor, derived
    > > subobject is already destroyed! But i have read some where
    > > that (1) ends with crash, second one is not.


    > PS:it is also possible that a null function exeption is
    > automatically neglected .


    There is no such thing as a null function exception. (For that
    matter, there is no such thing as a null function.)

    It's undefined behavior, so anything can happen. From a QoI
    point of view, I would expect the program to crash (assertion
    failure, segment violation or such---either the code tries to
    dereference a null pointer, or the compiler inserts a pointer to
    a function which aborts with an error message).

    --
    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, Feb 8, 2009
    #8
  9. James Kanze Guest

    On Feb 8, 8:04 am, ""
    <> wrote:
    > On Feb 7, 9:40 pm, terminator <> wrote:


    > > On Feb 7, 2:27 pm, ""


    > > <> wrote:
    > > > 1) Can we call pure virtual functions from base class destructor?
    > > > 2) Can we call virtual functions from base class destructor? which
    > > > function will be invoked?


    > > > What i think is either of them ends with crashing. because
    > > > when destructing the base class destructor, derived
    > > > subobject is already destroyed! But i have read some where
    > > > that (1) ends with crash, second one is not.


    > > PS:it is also possible that a null function exeption is
    > > automatically neglected .


    > Hmm..if we call virtual functions from base class functions it
    > would call dervied class implementation


    > but in i call virtual functions from base class destructor it
    > would call base class functions?


    If they exist and are not pure virtual. Once you've reached the
    base class destructor, the derived class object doesn't exist
    any more, and it makes no sense (and is extremely dangerous) to
    call a member function on something that doesn't exist.

    > Why the compilers dont behave as normaly user predicts? I
    > think most of the people would think otherwise in this case i
    > guess..


    And why would people think that you would call a function on
    something that doesn't exist?

    --
    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, Feb 8, 2009
    #9
  10. terminator Guest

    On Feb 8, 1:31 pm, James Kanze <> wrote:
    > On Feb 7, 5:40 pm, terminator <> wrote:
    >
    > > On Feb 7, 2:27 pm, ""
    > > <> wrote:
    > > > 1) Can we call pure virtual functions from base class destructor?
    > > > 2) Can we call virtual functions from base class destructor? which
    > > > function will be invoked?
    > > > What i think is either of them ends with crashing. because
    > > > when destructing the base class destructor, derived
    > > > subobject is already destroyed! But i have read some where
    > > > that (1) ends with crash, second one is not.

    > > PS:it is also possible that a null function exeption is
    > > automatically neglected .

    >
    > There is no such thing as a null function exception.  (For that
    > matter, there is no such thing as a null function.)
    >
    > It's undefined behavior, so anything can happen.  From a QoI
    > point of view, I would expect the program to crash (assertion
    > failure, segment violation or such---either the code tries to
    > dereference a null pointer, or the compiler inserts a pointer to
    > a function which aborts with an error message).
    >


    ok.my hands are up.**UB==UB**.

    thanks,
    FM.
     
    terminator, Feb 8, 2009
    #10
  11. Guest

    On Feb 8, 3:34 pm, James Kanze <> wrote:
    > On Feb 8, 8:04 am, ""
    >
    >
    >
    >
    >
    > <> wrote:
    > > On Feb 7, 9:40 pm, terminator <> wrote:
    > > > On Feb 7, 2:27 pm, ""
    > > > <> wrote:
    > > > > 1) Can we call pure virtual functions from base class destructor?
    > > > > 2) Can we call virtual functions from base class destructor? which
    > > > > function will be invoked?
    > > > > What i think is either of them ends with crashing. because
    > > > > when destructing the base class destructor, derived
    > > > > subobject is already destroyed! But i have read some where
    > > > > that (1) ends with crash, second one is not.
    > > > PS:it is also possible that a null function exeption is
    > > > automatically neglected .

    > > Hmm..if we call virtual functions from base class functions it
    > > would call dervied class implementation
    > > but in i call virtual functions from base class destructor it
    > > would call base class functions?

    >
    > If they exist and are not pure virtual.  Once you've reached the
    > base class destructor, the derived class object doesn't exist
    > any more, and it makes no sense (and is extremely dangerous) to
    > call a member function on something that doesn't exist.
    >
    > > Why the compilers dont behave as normaly user predicts? I
    > > think most of the people would think otherwise in this case i
    > > guess..

    >
    > And why would people think that you would call a function on
    > something that doesn't exist?
    >
    > --
    > 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- Hide quoted text -
    >
    > - Show quoted text -


    but people would never expect that compiler will show some extra
    intelligency and call base class functions! similar type of question
    found in one of the C++ interview. 99% of them answered calling
    virtual functions inside base class destructoe leads to crash...[that
    means most of the people think that it derived object is destroyed and
    any invocation of virtual function would crash]
     
    , Feb 9, 2009
    #11
  12. Ian Collins Guest

    wrote:
    > On Feb 8, 3:34 pm, James Kanze <> wrote:
    >> On Feb 8, 8:04 am, ""
    >> <> wrote:
    >>
    >>> Why the compilers dont behave as normaly user predicts? I
    >>> think most of the people would think otherwise in this case i
    >>> guess..

    >> And why would people think that you would call a function on
    >> something that doesn't exist?
    >>

    Please don't quote signatures (eben malformed ones like James'!).
    >
    > but people would never expect that compiler will show some extra
    > intelligency and call base class functions! similar type of question
    > found in one of the C++ interview. 99% of them answered calling
    > virtual functions inside base class destructoe leads to crash...[that
    > means most of the people think that it derived object is destroyed and
    > any invocation of virtual function would crash]


    Destroying an object is like pealing an onion; each layer is stripped
    away until you reach the base class. When you get there, the object is
    an instance of the base.

    --
    Ian Collins
     
    Ian Collins, Feb 9, 2009
    #12
    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. Dave
    Replies:
    2
    Views:
    4,160
    Donovan Rebbechi
    May 16, 2005
  2. frs
    Replies:
    20
    Views:
    756
    Alf P. Steinbach
    Sep 21, 2005
  3. Raider
    Replies:
    3
    Views:
    375
    Tom Widmer
    Apr 21, 2006
  4. Klaib

    Vitual memory problem

    Klaib, Feb 10, 2008, in forum: C++
    Replies:
    5
    Views:
    728
    Ralph D. Ungermann
    Feb 14, 2008
  5. Manjunatha

    Cookies not working in japanese vitual folder.

    Manjunatha, Aug 3, 2004, in forum: ASP General
    Replies:
    0
    Views:
    115
    Manjunatha
    Aug 3, 2004
Loading...

Share This Page