Delete a class with multiple inheritance through base pointer?

Discussion in 'C++' started by Ian, Feb 12, 2004.

  1. Ian

    Ian Guest

    Given

    struct A{...};
    struct B{...};

    struct C : public A, public B {...}

    int main()
    {
    A* a = new C;

    delete a;

    return 0;
    }

    Will the B part of a be correctly deleted?

    Ian
    Ian, Feb 12, 2004
    #1
    1. Advertising

  2. "Ian" <> wrote in message
    news:...
    >
    > Given
    >
    > struct A{...};
    > struct B{...};
    >
    > struct C : public A, public B {...}
    >
    > int main()
    > {
    > A* a = new C;
    >
    > delete a;
    >
    > return 0;
    > }
    >
    > Will the B part of a be correctly deleted?
    >
    > Ian
    >


    Its not a question of whether or not the B part will be correctly deleted.
    Its a question of whether the code is correct or not, if it is not the
    consequences are undefined.

    The answer is that the code is correct if A has a virtual destructor,
    otherwise you have undefined behaviour.

    john
    John Harrison, Feb 12, 2004
    #2
    1. Advertising

  3. Ian wrote:
    >
    > Given
    >
    > struct A{...};
    > struct B{...};
    >
    > struct C : public A, public B {...}
    >
    > int main()
    > {
    > A* a = new C;
    >
    > delete a;
    >
    > return 0;
    > }
    >
    > Will the B part of a be correctly deleted?


    only if A has a virtual destructor.

    i.e.

    struct A{ virtual ~A(){} ...}; // not the virtual destructor
    struct B{...};

    struct C : public A, public B {...}

    int main()
    {
    A* a = new C;

    delete a;

    return 0;
    }

    >
    > Ian
    >
    Gianni Mariani, Feb 12, 2004
    #3
  4. Ian

    Ian Guest

    I left out the virtual destructors in A and B...

    Should work, but I am seeing problems where it does not.

    Ian wrote:
    >
    > Given
    >
    > struct A{...};
    > struct B{...};
    >
    > struct C : public A, public B {...}
    >
    > int main()
    > {
    > A* a = new C;
    >
    > delete a;
    >
    > return 0;
    > }
    >
    > Will the B part of a be correctly deleted?
    >
    > Ian
    >
    Ian, Feb 12, 2004
    #4
  5. "Ian" <> wrote in message
    news:...
    > I left out the virtual destructors in A and B...
    >
    > Should work, but I am seeing problems where it does not.
    >


    What problems are you seeing?

    john
    John Harrison, Feb 12, 2004
    #5
  6. Ian

    Ian Guest

    John Harrison wrote:
    > "Ian" <> wrote in message
    > news:...
    >
    >>I left out the virtual destructors in A and B...
    >>
    >>Should work, but I am seeing problems where it does not.
    >>

    >
    >
    > What problems are you seeing?
    >

    What I think is a compiler bug.... Members of one base being invalid in
    the destructor of the derived class.

    Ian
    Ian, Feb 12, 2004
    #6
  7. "Ian" <> wrote in message
    news:...
    > John Harrison wrote:
    > > "Ian" <> wrote in message
    > > news:...
    > >
    > >>I left out the virtual destructors in A and B...
    > >>
    > >>Should work, but I am seeing problems where it does not.
    > >>

    > >
    > >
    > > What problems are you seeing?
    > >

    > What I think is a compiler bug.... Members of one base being invalid in
    > the destructor of the derived class.
    >
    > Ian
    >


    Compiler bugs are always a possibility but the code you posted is very
    simple (maybe your real code is more complex) so I'd be surprised if a
    compiler got that wrong.

    john
    John Harrison, Feb 12, 2004
    #7
  8. Ian

    Chris Theis Guest

    "Ian" <> wrote in message
    news:...
    > John Harrison wrote:
    > > "Ian" <> wrote in message
    > > news:...
    > >
    > >>I left out the virtual destructors in A and B...
    > >>
    > >>Should work, but I am seeing problems where it does not.
    > >>

    > >
    > >
    > > What problems are you seeing?
    > >

    > What I think is a compiler bug.... Members of one base being invalid in
    > the destructor of the derived class.
    >


    Could you elaborate on that (probably with some sample code & which compiler
    you're using). From your code I do not see a problem (with the virtual
    declartions being added) as the order of destruction is simply the reverse
    order of construction. Thus the dtor of the derived class is called before
    the one of the base class.

    Regards
    Chris
    Chris Theis, Feb 12, 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. tirath
    Replies:
    3
    Views:
    688
    Ivan Vecerina
    Oct 12, 2003
  2. Banaticus  Bart
    Replies:
    9
    Views:
    600
    Gary Labowitz
    May 7, 2004
  3. cppsks
    Replies:
    0
    Views:
    798
    cppsks
    Oct 27, 2004
  4. Zycor
    Replies:
    3
    Views:
    5,087
    David Harmon
    Dec 17, 2004
  5. Alberto Luaces
    Replies:
    6
    Views:
    373
    Fraser Ross
    Jun 19, 2006
Loading...

Share This Page