mulitple inheritance and constructors?

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

    mulitple inheritance and constructors?

    Hello,

    I just tried something with multiple inheritance and I have a problem with
    the construction of my object.

    There's a base class Base, containing an integer. The base class has 2
    derived classes: Derived1 and Derived2. Then, multiple inheritance is used
    to derive MultipleDer from Derived1 and Derived2. All classes have 1
    constructor: ClassName::Clas sName(int i = 0);

    The argument i is always passed to a class's base class, but when the code
    is run, the default value for class Base is used. Does anybody know why?

    Here's the code:

    #include<iostre am>
    using namespace std;

    class Base {
    int i;
    public:
    Base(int ii=12345) {i=ii;}
    void set(int ii) {i=ii;}
    int get() const {return i;}
    };

    class Derived1: public virtual Base {
    public:
    Derived1(int i=0): Base(i) {;}
    };

    class Derived2: public virtual Base {
    public:
    Derived2(int i=0): Base(i) {;}
    };

    class MultipleDer: public Derived1, public Derived2 {
    public:
    MultipleDer(int i=0): Derived1(i), Derived2(i) {;}
    void set1(int i) {Derived1::set( i);}
    void set2(int i) {Derived2::set( i);}
    int get1() const {return Derived1::get() ;}
    int get2() const {return Derived2::get() ;}
    };

    int main() {
    MultipleDer x(100);
    cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;

    x.set(0);
    cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;

    x.set1(1);
    cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;

    x.set2(2);
    cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;

    return 0;
    }

    Thanks for any help!

    Jochen


  • WW

    #2
    Re: mulitple inheritance and constructors?

    Jochen Zeischka wrote:[color=blue]
    > Hello,
    >
    > I just tried something with multiple inheritance and I have a problem
    > with the construction of my object.
    >
    > There's a base class Base, containing an integer. The base class has 2
    > derived classes: Derived1 and Derived2. Then, multiple inheritance is
    > used to derive MultipleDer from Derived1 and Derived2. All classes
    > have 1 constructor: ClassName::Clas sName(int i = 0);
    >
    > The argument i is always passed to a class's base class, but when the
    > code is run, the default value for class Base is used. Does anybody
    > know why?[/color]

    IIRC in case of virtual inheritance it is *always* the most derived class,
    which constructs the virtual base(s). Since in your case it is MultipleDer
    and it uses the default constructor (ehem, the one with the default
    argument) you will get that number.

    --
    WW aka Attila


    Comment

    • Mark Kerns

      #3
      Re: mulitple inheritance and constructors?

      > I just tried something with multiple inheritance and I have a problem with[color=blue]
      > the construction of my object.
      >
      > There's a base class Base, containing an integer. The base class has 2
      > derived classes: Derived1 and Derived2. Then, multiple inheritance is used
      > to derive MultipleDer from Derived1 and Derived2. All classes have 1
      > constructor: ClassName::Clas sName(int i = 0);
      >
      > The argument i is always passed to a class's base class, but when the code
      > is run, the default value for class Base is used. Does anybody know why?[/color]

      Virtual base class constructors are called by the constructor of the most
      derived class only. The intermediate calls are ignored. IOW, you need to
      invoke the constructor from within your most derived class. Read up on this
      for more info.


      Comment

      • Jochen Zeischka

        #4
        Re: mulitple inheritance and constructors?

        OK, thanks!

        My code runs fine now, but I understand that I have some more reading to do.

        Thanks a lot,

        Jochen

        "Mark Kerns" <_no_spam@nospa m.com> wrote in message
        news:N%Bgb.4446 4$ko%.19685@new s04.bloor.is.ne t.cable.rogers. com...[color=blue][color=green]
        > > I just tried something with multiple inheritance and I have a problem[/color][/color]
        with[color=blue][color=green]
        > > the construction of my object.
        > >
        > > There's a base class Base, containing an integer. The base class has 2
        > > derived classes: Derived1 and Derived2. Then, multiple inheritance is[/color][/color]
        used[color=blue][color=green]
        > > to derive MultipleDer from Derived1 and Derived2. All classes have 1
        > > constructor: ClassName::Clas sName(int i = 0);
        > >
        > > The argument i is always passed to a class's base class, but when the[/color][/color]
        code[color=blue][color=green]
        > > is run, the default value for class Base is used. Does anybody know why?[/color]
        >
        > Virtual base class constructors are called by the constructor of the most
        > derived class only. The intermediate calls are ignored. IOW, you need to
        > invoke the constructor from within your most derived class. Read up on[/color]
        this[color=blue]
        > for more info.
        >
        >[/color]


        Comment

        • Sim Nanda

          #5
          Re: mulitple inheritance and constructors?

          You can get the result you want if you change your MultipleDer ctor to
          MultipleDer(int i=0): Base(i), Derived1(i), Derived2(i) {;}

          Odd syntax. It's because the most derived class is responsible for
          constructing a virtual class.

          bye,
          Slarty


          "Jochen Zeischka" <jochen.zeischk a@rug.ac.be> wrote in message news:<bluiib$16 u$1@gaudi2.UGen t.be>...[color=blue]
          > Hello,
          >
          > I just tried something with multiple inheritance and I have a problem with
          > the construction of my object.
          >
          > There's a base class Base, containing an integer. The base class has 2
          > derived classes: Derived1 and Derived2. Then, multiple inheritance is used
          > to derive MultipleDer from Derived1 and Derived2. All classes have 1
          > constructor: ClassName::Clas sName(int i = 0);
          >
          > The argument i is always passed to a class's base class, but when the code
          > is run, the default value for class Base is used. Does anybody know why?
          >
          > Here's the code:
          >
          > #include<iostre am>
          > using namespace std;
          >
          > class Base {
          > int i;
          > public:
          > Base(int ii=12345) {i=ii;}
          > void set(int ii) {i=ii;}
          > int get() const {return i;}
          > };
          >
          > class Derived1: public virtual Base {
          > public:
          > Derived1(int i=0): Base(i) {;}
          > };
          >
          > class Derived2: public virtual Base {
          > public:
          > Derived2(int i=0): Base(i) {;}
          > };
          >
          > class MultipleDer: public Derived1, public Derived2 {
          > public:
          > MultipleDer(int i=0): Derived1(i), Derived2(i) {;}
          > void set1(int i) {Derived1::set( i);}
          > void set2(int i) {Derived2::set( i);}
          > int get1() const {return Derived1::get() ;}
          > int get2() const {return Derived2::get() ;}
          > };
          >
          > int main() {
          > MultipleDer x(100);
          > cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
          >
          > x.set(0);
          > cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
          >
          > x.set1(1);
          > cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
          >
          > x.set2(2);
          > cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
          >
          > return 0;
          > }
          >
          > Thanks for any help!
          >
          > Jochen[/color]

          Comment

          • Dan Cernat

            #6
            Re: mulitple inheritance and constructors?

            Do not Top Post in this NG.

            sslartybart@hot mail.com (Sim Nanda) wrote in message news:<31135f9a. 0310071400.705c dec8@posting.go ogle.com>...[color=blue]
            > You can get the result you want if you change your MultipleDer ctor to
            > MultipleDer(int i=0): Base(i), Derived1(i), Derived2(i) {;}[/color]
            ^
            what is the semicolon for?
            [color=blue]
            >
            > Odd syntax. It's because the most derived class is responsible for
            > constructing a virtual class.
            >
            > bye,
            > Slarty
            >[/color]


            Dan

            Comment

            Working...