mulitple inheritance and constructors?

  • Thread starter Jochen Zeischka
  • Start date
J

Jochen Zeischka

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::ClassName(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<iostream>
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
 
W

WW

Jochen said:
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::ClassName(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?

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.
 
M

Mark Kerns

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::ClassName(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?

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.
 
J

Jochen Zeischka

OK, thanks!

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

Thanks a lot,

Jochen
 
S

Sim Nanda

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
 
D

Dan Cernat

Do not Top Post in this NG.

You can get the result you want if you change your MultipleDer ctor to
MultipleDer(int i=0): Base(i), Derived1(i), Derived2(i) {;}
^
what is the semicolon for?
Odd syntax. It's because the most derived class is responsible for
constructing a virtual class.

bye,
Slarty


Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top