virtual inheritance

i have this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class A{
public:
	int a;
	A() {a=34;};
	A(int i){a=i;};
	virtual void print(){cout<<a;}
};
class B:virtual public A
{
public:
	int b;
	B() {b=64;};
	B(int i){b=i;};
	virtual void print(){cout<<b;}
};
class C:virtual public A
{
public:
	int c;
	C():A(1) {c=64;};
	C(int i){c=i;};
	virtual void print(){cout<<c;}
};
class D:public B,public C
{
public:
	int d;
	D():C() {d=64;};
	D(int i){d=i;};
	virtual void print(){cout<<d;}
};
void main( void )
{

D d;
cout<<d.a;
int k;
cin>>k;
}


in line 28,20 i try to give the valua 1 to A
but it doesnt take this value and it prints 34

i try to understand the class virtual inheritance..
vagelis, I hate to be rude, but I really think your should post your questions to 'Beginners' instead of this one. Anyway, your problem in this case, is that you introduce a new integer into each derived class, instead of reusing the 'a' from 'A'. Here is what I think you meant:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class A{
public:
	int a;
	A() {a=34;};
	A(int i){a=i;};
	virtual void print(){cout<<a;}
};
class B:virtual public A
{
public:
	B() {a=64;};
	B(int i){a=i;};
	virtual void print(){cout<<a;}
};
class C:virtual public A
{
public:
	C():A(1) {a=64;};
	C(int i){a=i;};
	virtual void print(){cout<<a;}
};
class D:public B,public C
{
public:
	D():C() {a=64;};
	D(int i){a=i;};
	virtual void print(){cout<<a;}
};
void main( void )
{

D d;
cout<<d.a;
int k;
cin>>k;
}
kspangsege, you are wrong - this not ridiculously simple and you are wrong - this has nothing to do with additional variables.

vagelis, you may want to read http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.11 and bellow. The key sentence is "there are special rules to make sure the virtual base class's constructor and destructor get called exactly once per instance". Since D() called A(), C() will not call A(1).

by the way, mistakes in your code: main must be int and return 0, ( void ) is only needed in C, you don't need a ; after defining a constructor (like any other function), instead of using cin >> integer for pausing, which is .. odd, use cin.get() if you don't use cin >> anywhre in your code, or cin.ignore().get() if you do.
Oh, I see my error - sorry!
"there are special rules to make sure the virtual base class's constructor and destructor get called exactly once per instance". Since D() called A(), C() will not call A(1).

ok i thought also that the answer would be somthing like this..
but i didnt know the answer and so i asked..

i dont have problem to ask in beginners because i am
but i think the questions are not so simple

anyway..
i will do what do you want..

thanks a lot..
So, the rule of thumb is this:

If a class A inherits multiple times and virtually from a class B, then A must call B's constructor directly, so if you do not write an explicit call to B's constructor, the default constructor, that takes no arguments, will be called.

This is not trivial - I totally agree.
sory another question about that
does the same rule about only one call of each constructor apply to not virtual classes??
You should think about it this way:

When a class A inherits multiple times from a class B, and the inheritance is not virtual, then A actually contains B multiple times, and therefore, in this case, B's constructor will be called multiple times - once for each instance.
ok thanks a lot..
Topic archived. No new replies allowed.