do I need a virtual destrucotr?

Discussion in 'C++' started by Jimmy Johns, Aug 17, 2003.

  1. Jimmy Johns

    Jimmy Johns Guest

    Hi all, I have some class hierarchy as follows:

    class S {};
    class A {};
    class B {public: vector<S*> v_; virtual ~B();};

    class C : public virtual A, public virtual B { // do I need to define
    virtual ~C() so that B can be properly destructed?};

    in fact, I don't even know if a destructor in C is even needed if I don't do
    any memory allocation? Thanks in advance.

    jj.
     
    Jimmy Johns, Aug 17, 2003
    #1
    1. Advertising

  2. Jimmy Johns

    Jimmy Johns Guest

    Okay, so for clarification purposes, suppose I have the following:
    base (pure virtual)
    ^
    |
    |
    poly derived1
    ^
    |
    |
    derived2

    and base, derived1, derived2 do not contain anything that needs to be
    specially allocated, deallocated. Then I do something like:

    base* p = new derived2;

    then I do:
    delete p;

    then only base's destructor will be called... correct? But that's okay,
    since stuff inside derived1 and derived2 (such as vector<>, list<>, etc)
    should take care of themselves? Or is it necessary to define the destructor
    explicitly, and call
    vector<>.clear() for vectors, lists... etc? It seems extrememly tedious to
    go throught the inheritance hierarchy and clearing objects that shouldn't
    have to be cleared. Any suggestions? Maybe I'm just confusing myself.

    jj

    "Rolf Magnus" <> wrote in message
    news:bhnlke$lch$06$-online.com...
    > Jimmy Johns wrote:
    >
    > > Hi all, I have some class hierarchy as follows:
    > >
    > > class S {};
    > > class A {};
    > > class B {public: vector<S*> v_; virtual ~B();};
    > >
    > > class C : public virtual A, public virtual B { // do I need to define
    > > virtual ~C() so that B can be properly destructed?};

    >
    > That depends. If it's possible that you destroy a C through a pointer to
    > one of its base classes, you need a virtual destructor, but you will
    > need that in the base class.
    >
    > > in fact, I don't even know if a destructor in C is even needed if I
    > > don't do any memory allocation?

    >
    > If you don't need a destructor, don't define one, except if you're in a
    > base class that is supposed to be used polymorphically, becase then
    > there is a chance that a derived object is deleted through a pointer to
    > base, so you'll need a virtual destructor in that base.
    >
     
    Jimmy Johns, Aug 17, 2003
    #2
    1. Advertising

  3. Jimmy Johns

    Rolf Magnus Guest

    Jimmy Johns wrote:

    > Okay, so for clarification purposes, suppose I have the following:
    > base (pure virtual)


    What is pure virtual? The destructor?

    > ^
    > |
    > |
    > poly derived1
    > ^
    > |
    > |
    > derived2
    >
    > and base, derived1, derived2 do not contain anything that needs to be
    > specially allocated, deallocated. Then I do something like:
    >
    > base* p = new derived2;
    >
    > then I do:
    > delete p;
    >
    > then only base's destructor will be called... correct?


    If you mean above that base's destructor is virtual, then no. derived2's
    destructor will be called. If OTOH, base's destructor is not virtual,
    then you are correct.

    > But that's okay, since stuff inside derived1 and derived2 (such as
    > vector<>, list<>, etc) should take care of themselves?


    What do you mean by "take care of themselves"? Their destructors must be
    called by someone. If derived2's destructor is called, the destructors
    of its member variables are called too.

    > Or is it necessary to define the destructor explicitly, and call
    > vector<>.clear() for vectors, lists... etc?


    Their destructors will do that when (and if) they are destroyed
    properly.

    > It seems extrememly tedious to go throught the inheritance hierarchy
    > and clearing objects that shouldn't have to be cleared. Any
    > suggestions? Maybe I'm just confusing myself.


    I guess you are. It's actually rather simple: If there is a chance (even
    if very low) that an instance of derived1 or derived2 is deleted
    through a pointer to base, base's destructor must be virtual. Then the
    correct destructor is automagically called.
     
    Rolf Magnus, Aug 17, 2003
    #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. Derek Simmons
    Replies:
    0
    Views:
    826
    Derek Simmons
    Aug 1, 2004
  2. Jéjé
    Replies:
    2
    Views:
    411
    Jéjé
    Nov 30, 2005
  3. while_1
    Replies:
    2
    Views:
    654
    Roedy Green
    Jun 25, 2004
  4. Jim Fischer
    Replies:
    3
    Views:
    462
    Jim Fischer
    Jul 31, 2003
  5. a
    Replies:
    7
    Views:
    383
    dasjotre
    Jun 28, 2007
Loading...

Share This Page