virtual inheritance

S

sksjava

#include <iostream>
struct parent {
parent() : integer(0) { cout << "parent default\n"; }
parent(int i) : integer(i) { cout << "parent non-default\n"; }
int integer;
};

struct child1 : public parent {
child1() { cout << "child1 default\n"; }
child1(int i) : parent(i) { cout << "child1 non-default\n"; }
};

struct gchild : public child1 {
gchild(int i) : child1(i) { cout << "gchild non-default\n"; }
};

int main() {
gchild g(10);
cout << g.integer << endl;
}


The above code prints:
parent non-default
child1 non-default
gchild non-default
10


Modifying the line "struct child1 : public parent" to "struct child1 :
public virtual parent" will give the following result:
parent default
child1 non-default
gchild non-default
0


I am totally confused on what the compiler is doing. Thanks in advance.
 
M

Michael Mellor

sksjava said:
#include <iostream>

using std::cout;
using std::endl;
struct parent {
parent() : integer(0) { cout << "parent default\n"; }
parent(int i) : integer(i) { cout << "parent non-default\n"; }
int integer;
};

struct child1 : public parent {
child1() { cout << "child1 default\n"; }
child1(int i) : parent(i) { cout << "child1 non-default\n"; }
};

struct gchild : public child1 {
gchild(int i) : child1(i) { cout << "gchild non-default\n"; }
};

int main() {
gchild g(10);
cout << g.integer << endl;
}


The above code prints:
parent non-default
child1 non-default
gchild non-default
10


Modifying the line "struct child1 : public parent" to "struct child1 :
public virtual parent" will give the following result:
parent default
child1 non-default
gchild non-default
0


I am totally confused on what the compiler is doing. Thanks in advance.
In virtual inheritence the most derived type constructs the virtually
inherited object(s). Therefore the following should print the required
result:
struct gchild : public child1 {
gchild(int i) : child1(i), parent(10)
{
cout << "gchild non-default\n";
}
};
If you had removed the parent's default ctor it wouldn't have compiled.

Take the following example:
struct child1 : virtual public parent {
child1() : parent(20) { cout << "child1 default\n"; }
};

struct child2 : virtual public parent {
child2() : parent(10) { cout << "child2 default\n"; }
};

struct gchild : public child1, public child2 {
gchild() { cout << "gchild non-default\n"; }
};
what value should parent.integer be? Hence you have to call an
appropriate parent ctor in all the derived types.

Mike
 
B

buildsoc

i think that the compiler produced some code to invoke parent's default
constructor( parent() ) that locate the position of parent subject in the
gchild object.
these codes were put into both of child constructors and do some job like
landing party.
so the parent(int i) was not invoked.


~ Let us linux ~
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top