the old virtual destructor nut

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

  1. grahamo

    grahamo Guest

    Hi,

    I recently did a test which required the user to note that a base
    class had a non virtual destructor. Hence the dtor wouldn't be called
    when the object was deleted.


    The code is below (from memory) it's straightforward enough. My
    question is this;

    Does the spec say that the base classes dtor *wont* be called, or that
    the behavior is undefined....The difference is subtle but all the same
    its there. Am I correct in thinking that the behaviour is undefined?
    According to Scott Meyers, it's undefined. I don't have a copy of the
    spec but I'd like to get clarification.

    thanks mulcho.

    GrahamO


    #include <iostream>

    using namespace std;

    class base
    {
    public:

    base()
    {
    cout << "base()" << endl;
    }

    ~base()
    {
    cout << "~base() " << endl;
    }
    };

    class derived : public base
    {
    public:

    derived()
    {
    cout << "derived()" << endl;
    }

    ~derived()
    {
    cout << "~derived() " << endl;
    }
    };

    derived* makeDerived()
    {
    return new derived();
    };

    base* makeBase()
    {
    return new base();
    };

    void cleanUp(base* b)
    {
    delete b;
    }


    int main(int argc, char** argv)
    {

    base* b = makeBase();
    derived* d = makeDerived();

    cleanUp(b);
    cleanUp(d);


    return 0;
    }
    grahamo, Jul 25, 2004
    #1
    1. Advertising

  2. grahamo

    Max M. Guest

    grahamo wrote:

    > The difference is subtle but all the same
    > its there. Am I correct in thinking that the behaviour is undefined?


    You're correct. The difference is NOT subtle, though.
    With my compiler, on my platform, the following code

    class B1 { };
    class B2 { }; // no virtual dtor!
    class D: public B1, public B2 { };
    int main()
    {
    D* dp = new D;
    B2* b2p = dp;
    delete b2p;
    }

    causes a segmentation fault.
    (Note that multiple inheritance is involved.)

    Max
    Max M., Jul 25, 2004
    #2
    1. Advertising

  3. grahamo

    grahamo Guest

    Thanks a million for that Max. All I have to do now is let the guy
    that interviewed me know that his test is wrong.. ! :) :) Better wait
    until I actually get the job though:)


    cheers mate

    GrahamO

    "Max M." <2000.it> wrote in message news:<ce150t$75l$>...
    > grahamo wrote:
    >
    > > The difference is subtle but all the same
    > > its there. Am I correct in thinking that the behaviour is undefined?

    >
    > You're correct. The difference is NOT subtle, though.
    > With my compiler, on my platform, the following code
    >
    > class B1 { };
    > class B2 { }; // no virtual dtor!
    > class D: public B1, public B2 { };
    > int main()
    > {
    > D* dp = new D;
    > B2* b2p = dp;
    > delete b2p;
    > }
    >
    > causes a segmentation fault.
    > (Note that multiple inheritance is involved.)
    >
    > Max
    grahamo, Jul 26, 2004
    #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. Ken
    Replies:
    2
    Views:
    1,191
    John Saunders
    Oct 22, 2003
  2. Soren Kuula

    Computational nut?

    Soren Kuula, Feb 19, 2006, in forum: XML
    Replies:
    1
    Views:
    401
    Soren Kuula
    Feb 19, 2006
  3. Calvin Lai
    Replies:
    7
    Views:
    538
    Calvin Lai
    Dec 18, 2003
  4. Chunhui Han
    Replies:
    2
    Views:
    487
  5. grahamo

    grr... this old nut

    grahamo, Jul 13, 2004, in forum: C++
    Replies:
    37
    Views:
    949
    Bob Hairgrove
    Aug 7, 2004
Loading...

Share This Page