why define virtual destructor

Discussion in 'C++' started by Tony Johansson, Aug 11, 2005.

  1. Hello Experts!!

    Assume I have a base class called animal. I want this class to be abstract
    so I make the destructor pure virtual by having this statement.
    virtual ~Animal() = 0;

    Destructor will never be inherited.

    Now to my question why do I have to give a body {} at least the empty body
    to the pure virtual destructor.

    What's the difference between
    virtual ~Animal() = 0; and virtual ~Animal() = 0 {};

    Normally when you have pure virtual destructors you almost never give any
    kind of body to the function.

    Many thanks.

    //Tony
     
    Tony Johansson, Aug 11, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Kai-Uwe Bux Guest

    Tony Johansson wrote:

    > Hello Experts!!
    >
    > Assume I have a base class called animal. I want this class to be abstract
    > so I make the destructor pure virtual by having this statement.
    > virtual ~Animal() = 0;
    >
    > Destructor will never be inherited.


    Huh?

    > Now to my question why do I have to give a body {} at least the empty body
    > to the pure virtual destructor.


    You don't.

    > What's the difference between
    > virtual ~Animal() = 0; and virtual ~Animal() = 0 {};


    virtual ~Animal() = 0;
    is valid C++.

    virtual ~Animal() = 0 {};
    will not compile.

    > Normally when you have pure virtual destructors you almost never give any
    > kind of body to the function.


    "almost"?


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 11, 2005
    #2
    1. Advertising

  3. Tony Johansson

    Srini Guest

    > Hello Experts!!
    >
    > Assume I have a base class called animal. I want this class to be abstract
    > so I make the destructor pure virtual by having this statement.
    > virtual ~Animal() = 0;
    >
    > Destructor will never be inherited.
    >
    > Now to my question why do I have to give a body {} at least the empty body
    > to the pure virtual destructor.


    This has been discussed earlier too...
    It is essential to define the pure virtual destructor of a base class.
    This is because, all the destructors in a class heirarchy are called
    during the destruction of a derived class object. If you could leave
    off the definition of a pure virtual destructor, what body will the
    compiler execute during destruction?

    >
    > What's the difference between
    > virtual ~Animal() = 0; and virtual ~Animal() = 0 {};
    >


    The second way of defining a pure virtual destructor is illegal.

    virtual ~Animal() = 0;

    Animal::~Animal() {
    // ...
    }

    > Normally when you have pure virtual destructors you almost never give any
    > kind of body to the function.
    >


    No. In fact, you have to provide a body to the pure virtual destructor.
    Otherwise you'll get linker errors - the linker would complain about
    an undefined reference to the destructor whenever there's a derived
    class object being destroyed.

    > Many thanks.
    >
    > //Tony


    Srini
     
    Srini, Aug 11, 2005
    #3
  4. Tony Johansson

    Kai-Uwe Bux Guest

    Re: [retraction] why define virtual destructor

    Sorry, I messed up:

    Kai-Uwe Bux wrote:

    > Tony Johansson wrote:
    >
    >> Hello Experts!!
    >>
    >> Assume I have a base class called animal. I want this class to be
    >> abstract so I make the destructor pure virtual by having this statement.
    >> virtual ~Animal() = 0;
    >>
    >> Destructor will never be inherited.

    >
    > Huh?
    >
    >> Now to my question why do I have to give a body {} at least the empty
    >> body to the pure virtual destructor.

    >
    > You don't.


    Actually, you do because C++ standard (12.4/7) says you shall at least if
    your program is going to create objects of type Animal (or any derived
    type). Now, if you would not want to create such objects, why would you
    have the class in the first place?

    >> What's the difference between
    >> virtual ~Animal() = 0; and virtual ~Animal() = 0 {};

    >
    > virtual ~Animal() = 0;
    > is valid C++.
    >
    > virtual ~Animal() = 0 {};
    > will not compile.
    >
    >> Normally when you have pure virtual destructors you almost never give any
    >> kind of body to the function.

    >
    > "almost"?


    Goes for pure virtual functions (otherwise, why make them pure in the first
    place), but destructors are special: they get called from destructors of
    derived classes. Thus, they better be defined even for an abstract base
    class. (That seems to be the rational behind 12.4/7.)


    Best

    Kai-Uwe
     
    Kai-Uwe Bux, Aug 11, 2005
    #4
  5. Tony Johansson

    Guest

    In general, you are allowed to give a body for any pure virtual
    function that you might define. You normally don't. If you do, your
    derived classes can call this code. You make pure virtual functions to
    create an interface only and require that individual implementation
    classes all inherit from it. However, if you want to provide a default
    implementation (which derived classes can call) along with your
    abstract base class, you do it using the body of the pure virtual
    function.
     
    , Aug 11, 2005
    #5
    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:
    564
    Calvin Lai
    Dec 18, 2003
  2. Chunhui Han
    Replies:
    2
    Views:
    514
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  4. frs
    Replies:
    20
    Views:
    776
    Alf P. Steinbach
    Sep 21, 2005
  5. arun
    Replies:
    2
    Views:
    555
    benben
    Jun 13, 2006
Loading...

Share This Page