Multiple Inheritance - does order matter?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    Multiple Inheritance - does order matter?

    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


  • Victor Bazarov

    #2
    Re: Multiple Inheritance - does order matter?

    "Mark" <mark_2811nospa m@excite.com> wrote...[color=blue]
    > 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[/color]
    "wrong".[color=blue]
    >
    > In my program I use an STL vector to store objects of type Server *.[/color]
    Server[color=blue]
    > 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[/color]
    Server[color=blue]
    > objects inherits from both Server and a class called Process. If I[/color]
    declare[color=blue]
    > 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?[/color]

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

    Victor


    Comment

    • Mark

      #3
      Re: Multiple Inheritance - does order matter?

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

      Mark

      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
      news:FYIKb.7615 79$Tr4.2169809@ attbi_s03...[color=blue]
      > "Mark" <mark_2811nospa m@excite.com> wrote...[color=green]
      > > When declaring a class that uses multiple inheritance, does the order[/color][/color]
      used[color=blue][color=green]
      > > 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[/color]
      > "wrong".[color=green]
      > >
      > > In my program I use an STL vector to store objects of type Server *.[/color]
      > Server[color=green]
      > > is an abstract base class. When exiting my program I iterate through[/color][/color]
      the[color=blue][color=green]
      > > vector and call delete on all my Server objects. One of the concrete[/color]
      > Server[color=green]
      > > objects inherits from both Server and a class called Process. If I[/color]
      > declare[color=green]
      > > 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?[/color]
      >
      > You probably forgot to declare 'Server's destructor virtual.
      >
      > Victor
      >
      >[/color]


      Comment

      • Victor Bazarov

        #4
        Re: Multiple Inheritance - does order matter?

        "Mark" <mark_2811nospa m@excite.com> wrote...[color=blue]
        > Hi Victor, that's what I thought too but all destructors are already
        > virtual. Maybe something else is going on ...[/color]

        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.
        [color=blue]
        > Mark
        >
        > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
        > news:FYIKb.7615 79$Tr4.2169809@ attbi_s03...[color=green]
        > > "Mark" <mark_2811nospa m@excite.com> wrote...[color=darkred]
        > > > When declaring a class that uses multiple inheritance, does the order[/color][/color]
        > used[color=green][color=darkred]
        > > > when listing the inheritance matter? I'm finding with my compiler[/color][/color][/color]
        (gcc[color=blue][color=green][color=darkred]
        > > > 3.2.2) that my program seg faults when destructing if the order is[/color]
        > > "wrong".[color=darkred]
        > > >
        > > > In my program I use an STL vector to store objects of type Server *.[/color]
        > > Server[color=darkred]
        > > > is an abstract base class. When exiting my program I iterate through[/color][/color]
        > the[color=green][color=darkred]
        > > > vector and call delete on all my Server objects. One of the concrete[/color]
        > > Server[color=darkred]
        > > > objects inherits from both Server and a class called Process. If I[/color]
        > > declare[color=darkred]
        > > > 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?[/color]
        > >
        > > You probably forgot to declare 'Server's destructor virtual.
        > >
        > > Victor
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • David Harmon

          #5
          Re: Multiple Inheritance - does order matter?

          On Tue, 6 Jan 2004 17:20:03 -0700 in comp.lang.c++, "Mark"
          <mark_2811nospa m@excite.com> was alleged to have written:[color=blue]
          >When declaring a class that uses multiple inheritance, does the order used
          >when listing the inheritance matter?[/color]

          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.

          Comment

          • Mark

            #6
            Re: Multiple Inheritance - does order matter?

            On Wed, 07 Jan 2004 02:49:19 +0000, David Harmon wrote:
            [color=blue]
            > On Tue, 6 Jan 2004 17:20:03 -0700 in comp.lang.c++, "Mark"
            > <mark_2811nospa m@excite.com> was alleged to have written:[color=green]
            >>When declaring a class that uses multiple inheritance, does the order used
            >>when listing the inheritance matter?[/color]
            >
            > 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.[/color]

            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

            Comment

            Working...