calling virtual destructors and virtual functions

Discussion in 'C++' started by marcwentink@hotmail.com, Jan 31, 2006.

  1. Guest

    Say I have a class A, and a class B that inherits from A. Now A (and B)
    has a virtual destructor and a virtual function F();

    If I now make these statements

    A* ptrA = new B;
    ptrA->F();
    delete ptrA

    then in the statement ptrA->F(), by means of the polymorph behavior,
    the F() of class B is called. But the F() of class B only. And in
    delete ptrA, both destructors of class A and B are called.

    Is this right? Because then the two statements behave differently.
    Somehow, for a moment, I was thinking that in the call ptrA->F(), both
    virtual functions of class A and B should be processed.
     
    , Jan 31, 2006
    #1
    1. Advertising

  2. * :
    > Say I have a class A, and a class B that inherits from A. Now A (and B)
    > has a virtual destructor and a virtual function F();
    >
    > If I now make these statements
    >
    > A* ptrA = new B;
    > ptrA->F();
    > delete ptrA
    >
    > then in the statement ptrA->F(), by means of the polymorph behavior,
    > the F() of class B is called. But the F() of class B only. And in
    > delete ptrA, both destructors of class A and B are called.
    >
    > Is this right? Because then the two statements behave differently.
    > Somehow, for a moment, I was thinking that in the call ptrA->F(), both
    > virtual functions of class A and B should be processed.


    A destructor is a _special_ member function. You can think of it as if
    the compiler inserts calls to destructors of data members and base
    classes at the end of the destructor. If you want that kind of behavior
    for other member functions you'll have to code it yourself.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 31, 2006
    #2
    1. Advertising

  3. On 31 Jan 2006 08:24:05 -0800, ""
    <> wrote:

    >Say I have a class A, and a class B that inherits from A. Now A (and B)
    >has a virtual destructor and a virtual function F();
    >
    >If I now make these statements
    >
    >A* ptrA = new B;
    >ptrA->F();
    >delete ptrA
    >
    >then in the statement ptrA->F(), by means of the polymorph behavior,
    >the F() of class B is called. But the F() of class B only. And in
    >delete ptrA, both destructors of class A and B are called.
    >
    >Is this right? Because then the two statements behave differently.
    >Somehow, for a moment, I was thinking that in the call ptrA->F(), both
    >virtual functions of class A and B should be processed.


    The destructor is a special function. A is a base class of B,
    therefore it is contained by B and has to be destroyed much like any
    other member variables of B have to be destroyed when B is destroyed.
    If you delete a B* directly, its A part is also deleted -- even when
    A's destructor is not virtual.

    But the destructor of A (and B) is virtual, so calling delete on an A*
    calls B's destructor, and during the normal destruction of B, A's
    destructor is also called. It is therefore the same mechanism as
    calling an ordinary function; the only difference is that the
    destructors are "chained".

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jan 31, 2006
    #3
  4. Nitin Motgi Guest

    wrote:
    > Say I have a class A, and a class B that inherits from A. Now A (and B)
    > has a virtual destructor and a virtual function F();
    >
    > If I now make these statements
    >
    > A* ptrA = new B;
    > ptrA->F();
    > delete ptrA
    >
    > then in the statement ptrA->F(), by means of the polymorph behavior,
    > the F() of class B is called. But the F() of class B only. And in


    Not untill you the make the F() in A as virtual. If you have it
    as virtual you would be calling the F() of B.

    > delete ptrA, both destructors of class A and B are called.

    Yes because the base class declares the destructor as virtual.

    >
    > Is this right? Because then the two statements behave differently.
    > Somehow, for a moment, I was thinking that in the call ptrA->F(), both
    > virtual functions of class A and B should be processed.


    No, this is not right. It's only the behaviour of constructors and
    destructor
    that is different.

    -- Nitin Motgi
     
    Nitin Motgi, Jan 31, 2006
    #4
    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. qazmlp
    Replies:
    7
    Views:
    514
    Howard
    Jul 27, 2004
  2. heted7
    Replies:
    33
    Views:
    1,078
    Chris Dearlove
    May 12, 2005
  3. Peter
    Replies:
    6
    Views:
    391
  4. Pravesh
    Replies:
    3
    Views:
    339
    Pravesh
    Jun 20, 2006
  5. Henrik Goldman
    Replies:
    6
    Views:
    626
    terminator
    Nov 19, 2006
Loading...

Share This Page