If declared as virtual in base, its derived version also is virtual. Why not for destructors?

Discussion in 'C++' started by qazmlp, Jul 25, 2004.

  1. qazmlp

    qazmlp Guest

    When a member function is declared as virtual in the base class, the
    derived class versions of it are always treated as virtual.

    I am just wondering, why the same concept was not used for the
    destructors.
    What I am expecting is, if the destructor is declared as virtual in
    base, the destructors of all its derived classes also should be
    virtual always.
    What exactly is the reason for not having it so?

    Thanks!
     
    qazmlp, Jul 25, 2004
    #1
    1. Advertising

  2. qazmlp wrote in news: in
    comp.lang.c++:

    > When a member function is declared as virtual in the base class, the
    > derived class versions of it are always treated as virtual.
    >
    > I am just wondering, why the same concept was not used for the
    > destructors.
    > What I am expecting is, if the destructor is declared as virtual in
    > base, the destructors of all its derived classes also should be
    > virtual always.
    > What exactly is the reason for not having it so?


    It is So, you have been missinformed.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jul 25, 2004
    #2
    1. Advertising

  3. qazmlp

    Phlip Guest

    qazmlp wrote:

    > When a member function is declared as virtual in the base class, the
    > derived class versions of it are always treated as virtual.
    >
    > I am just wondering, why the same concept was not used for the
    > destructors.
    > What I am expecting is, if the destructor is declared as virtual in
    > base, the destructors of all its derived classes also should be
    > virtual always.
    > What exactly is the reason for not having it so?


    Uh, constructors can't be virtual. Destructors inherit virtuality. What
    evidence are you working with?

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 25, 2004
    #3
  4. qazmlp

    JKop Guest

    qazmlp posted:

    > When a member function is declared as virtual in the base class, the
    > derived class versions of it are always treated as virtual.
    >
    > I am just wondering, why the same concept was not used for the
    > destructors.
    > What I am expecting is, if the destructor is declared as virtual in
    > base, the destructors of all its derived classes also should be
    > virtual always.
    > What exactly is the reason for not having it so?
    >
    > Thanks!



    Bullshit.


    class Mammal
    {
    public:

    virtual ~Mammal() {}
    }

    class Primate : public Mammal {};

    class Human : public Primate {};

    int main()
    {
    Mammal* john = new Human;

    delete john;
    }



    -JKop
     
    JKop, Jul 25, 2004
    #4
  5. qazmlp

    Phlip Guest

    JKop wrote:

    > Bullshit.


    One can usually masquerade such derision within false diplomacy...

    >
    > class Mammal
    > {
    > public:
    >
    > virtual ~Mammal() {}
    > }
    >
    > class Primate : public Mammal {};
    >
    > class Human : public Primate {};
    >
    > int main()
    > {
    > Mammal* john = new Human;
    >
    > delete john;
    > }


    That applies the test "it compiles, runs, and does not crash".

    To apply the test "my compiler interprets the standard like this", put a
    cout statement inside the derived destructor.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 25, 2004
    #5
  6. qazmlp

    JKop Guest

    Phlip posted:

    > To apply the test "my compiler interprets the standard

    like this", put a
    > cout statement inside the derived destructor.



    I prefered my way. If the person is bothered, they'll do
    what needs to be done. If they're not bothered, my
    assistance will have been of no benefit.


    -JKop
     
    JKop, Jul 25, 2004
    #6
  7. qazmlp

    David Rubin Guest

    JKop <> wrote in message news:<RnTMc.5496$>...

    [snip]
    > > What I am expecting is, if the destructor is declared as virtual in
    > > base, the destructors of all its derived classes also should be
    > > virtual always.
    > > What exactly is the reason for not having it so?


    > Bullshit.


    > class Mammal
    > {
    > public:


    > virtual ~Mammal() {}
    > }


    > class Primate : public Mammal {};


    > class Human : public Primate {};


    > int main()
    > {
    > Mammal* john = new Human;


    > delete john;
    > }


    How does this illustrate that the "virtual-ness" of destructors is inherited?

    /david
     
    David Rubin, Jul 26, 2004
    #7
  8. qazmlp

    Howard Guest

    "qazmlp" <> wrote in message
    news:...
    > When a member function is declared as virtual in the base class, the
    > derived class versions of it are always treated as virtual.
    >
    > I am just wondering, why the same concept was not used for the
    > destructors.
    > What I am expecting is, if the destructor is declared as virtual in
    > base, the destructors of all its derived classes also should be
    > virtual always.
    > What exactly is the reason for not having it so?
    >
    > Thanks!


    But it *is* so!

    Try this test: write a class A, with a virtual destructor. Write a class B
    publicly deriving from A, but declare its destructor *without* the virtual
    keyword. Then write a third class C publicly deriving from B, again without
    the virtual keyword in the destructor declaration. Put some cout code in
    each destructor to see which ones get called. In main (or some other
    function), declare a B* pointer, and instantiate it via "new C()". Then
    call delete on the pointer. You should see that the destructor for C is
    called. That's because the destructor for B is virtual, even though you
    didn't give it the virtual keyword. The only place you had the virtual
    keyword was in the A class, yet you're able to delete a C object via a
    pointer to a B object, which is only possible if the base class (B) has a
    virtual destructor. So B must have inherited the "virtualness" of its
    destructor from A. Q.E.D.

    (But, I would advise you to always add the virtual keyword to any function
    that overrides a base class' virtual function, including your destructors,
    just so you don't have to go hunting up the inheritance tree later to see if
    a function is inheriting virtual behavior or not.)

    -Howard
     
    Howard, Jul 27, 2004
    #8
    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. Xiangliang Meng
    Replies:
    2
    Views:
    414
    Jack Klein
    Jun 21, 2004
  2. qazmlp
    Replies:
    1
    Views:
    572
    qazmlp
    Apr 10, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,030
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    390
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page