Multiple Inheritance - does order matter?

Discussion in 'C++' started by Mark, Jan 7, 2004.

  1. Mark

    Mark Guest

    When declaring a class that uses multiple inheritance, does the order used
    when listing the inheritance matter? I'm finding with my compiler (gcc
    3.2.2) that my program seg faults when destructing if the order is "wrong".

    In my program I use an STL vector to store objects of type Server *. Server
    is an abstract base class. When exiting my program I iterate through the
    vector and call delete on all my Server objects. One of the concrete Server
    objects inherits from both Server and a class called Process. If I declare
    the class as:

    class MyServer : public Server, public Process
    {
    };

    it's fine, but if I declare it as:
    class MyServer : public Process, public Server
    {
    };

    my program seg faults when trying to call the destructor.

    Can anyone give me a hint as to what I'm doing wrong?

    Thanks,
    Mark
    Mark, Jan 7, 2004
    #1
    1. Advertising

  2. "Mark" <> wrote...
    > When declaring a class that uses multiple inheritance, does the order used
    > when listing the inheritance matter? I'm finding with my compiler (gcc
    > 3.2.2) that my program seg faults when destructing if the order is

    "wrong".
    >
    > In my program I use an STL vector to store objects of type Server *.

    Server
    > is an abstract base class. When exiting my program I iterate through the
    > vector and call delete on all my Server objects. One of the concrete

    Server
    > objects inherits from both Server and a class called Process. If I

    declare
    > the class as:
    >
    > class MyServer : public Server, public Process
    > {
    > };
    >
    > it's fine, but if I declare it as:
    > class MyServer : public Process, public Server
    > {
    > };
    >
    > my program seg faults when trying to call the destructor.
    >
    > Can anyone give me a hint as to what I'm doing wrong?


    You probably forgot to declare 'Server's destructor virtual.

    Victor
    Victor Bazarov, Jan 7, 2004
    #2
    1. Advertising

  3. Mark

    Mark Guest

    Hi Victor, that's what I thought too but all destructors are already
    virtual. Maybe something else is going on ...

    Mark

    "Victor Bazarov" <> wrote in message
    news:FYIKb.761579$Tr4.2169809@attbi_s03...
    > "Mark" <> wrote...
    > > When declaring a class that uses multiple inheritance, does the order

    used
    > > when listing the inheritance matter? I'm finding with my compiler (gcc
    > > 3.2.2) that my program seg faults when destructing if the order is

    > "wrong".
    > >
    > > In my program I use an STL vector to store objects of type Server *.

    > Server
    > > is an abstract base class. When exiting my program I iterate through

    the
    > > vector and call delete on all my Server objects. One of the concrete

    > Server
    > > objects inherits from both Server and a class called Process. If I

    > declare
    > > the class as:
    > >
    > > class MyServer : public Server, public Process
    > > {
    > > };
    > >
    > > it's fine, but if I declare it as:
    > > class MyServer : public Process, public Server
    > > {
    > > };
    > >
    > > my program seg faults when trying to call the destructor.
    > >
    > > Can anyone give me a hint as to what I'm doing wrong?

    >
    > You probably forgot to declare 'Server's destructor virtual.
    >
    > Victor
    >
    >
    Mark, Jan 7, 2004
    #3
  4. "Mark" <> wrote...
    > Hi Victor, that's what I thought too but all destructors are already
    > virtual. Maybe something else is going on ...


    Reduce your program to the bare minimum that still exhibits the error you
    posted about and then post the code, we could try to analyse it further.

    Of course, if it has too much OS-dependent code (forking, threading, or
    whatever) and removing it stops the error from re-occurring, it probably
    is not a language problem...

    And, please, don't top-post.

    > Mark
    >
    > "Victor Bazarov" <> wrote in message
    > news:FYIKb.761579$Tr4.2169809@attbi_s03...
    > > "Mark" <> wrote...
    > > > When declaring a class that uses multiple inheritance, does the order

    > used
    > > > when listing the inheritance matter? I'm finding with my compiler

    (gcc
    > > > 3.2.2) that my program seg faults when destructing if the order is

    > > "wrong".
    > > >
    > > > In my program I use an STL vector to store objects of type Server *.

    > > Server
    > > > is an abstract base class. When exiting my program I iterate through

    > the
    > > > vector and call delete on all my Server objects. One of the concrete

    > > Server
    > > > objects inherits from both Server and a class called Process. If I

    > > declare
    > > > the class as:
    > > >
    > > > class MyServer : public Server, public Process
    > > > {
    > > > };
    > > >
    > > > it's fine, but if I declare it as:
    > > > class MyServer : public Process, public Server
    > > > {
    > > > };
    > > >
    > > > my program seg faults when trying to call the destructor.
    > > >
    > > > Can anyone give me a hint as to what I'm doing wrong?

    > >
    > > You probably forgot to declare 'Server's destructor virtual.
    > >
    > > Victor
    > >
    > >

    >
    >
    Victor Bazarov, Jan 7, 2004
    #4
  5. Mark

    David Harmon Guest

    On Tue, 6 Jan 2004 17:20:03 -0700 in comp.lang.c++, "Mark"
    <> was alleged to have written:
    >When declaring a class that uses multiple inheritance, does the order used
    >when listing the inheritance matter?


    Your base classes are constructed in the order in which they are
    declared, and destructed in the reverse order. (The order in which they
    are mentioned in a constructor initializer list is irrelevant.) Other
    than that I don't think it should make any difference.
    David Harmon, Jan 7, 2004
    #5
  6. Mark

    Mark Guest

    On Wed, 07 Jan 2004 02:49:19 +0000, David Harmon wrote:

    > On Tue, 6 Jan 2004 17:20:03 -0700 in comp.lang.c++, "Mark"
    > <> was alleged to have written:
    >>When declaring a class that uses multiple inheritance, does the order used
    >>when listing the inheritance matter?

    >
    > Your base classes are constructed in the order in which they are
    > declared, and destructed in the reverse order. (The order in which they
    > are mentioned in a constructor initializer list is irrelevant.) Other
    > than that I don't think it should make any difference.


    Dumb error, I was too quick to dismiss the non-virtual destructor issue.
    One of my abstract base classes didn't have a destructor declared at all,
    just a few pure virtual methods that must be implemented. So when I added
    a trivial destructor it worked.

    Thanks!
    Mark
    Mark, Jan 7, 2004
    #6
    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. SpaceCowboy
    Replies:
    6
    Views:
    514
    SpaceCowboy
    Aug 14, 2003
  2. hilz
    Replies:
    1
    Views:
    501
    Richard Tobin
    Jan 18, 2006
  3. pete
    Replies:
    20
    Views:
    633
    Richard Bos
    Jun 13, 2005
  4. Brian Dude

    Does structure order matter?

    Brian Dude, Jan 22, 2006, in forum: C Programming
    Replies:
    8
    Views:
    291
    Brian Dude
    Jan 24, 2006
  5. Replies:
    2
    Views:
    1,463
    Joseph Kesselman
    Aug 10, 2007
Loading...

Share This Page