Virtual inheritance

Can someone please explain me why the out put of this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class G_d{
public:
    G_d() { cout<<" G_d cTor\n"; }
};
 
class Adam: virtual public G_d{
public:
        Adam() { cout<<"Adam cTor\n"; }
};
 
class Eve: public G_d{
public:
        Eve() { cout<<"Eve cTor\n"; }
};
 
class Son:  public Adam, public Eve{
public:
        Son() { cout<<"Son cTor\n"; }
};
 
int main(){
        Son s;
        return 0;
}


output:
 G_d cTor
Adam cTor
 G_d cTor
Eve cTor
Son cTor


is different from this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class G_d{
public:
    G_d() { cout<<" G_d cTor\n"; }
};
 
class Adam:  public G_d{
public:
        Adam() { cout<<"Adam cTor\n"; }
};
 
class Eve: virtual public G_d{
public:
        Eve() { cout<<"Eve cTor\n"; }
};
 
class Son:  public Adam, public Eve{
public:
        Son() { cout<<"Son cTor\n"; }
};
 
int main(){
        Son s;
        return 0;
}


output:
 G_d cTor
 G_d cTor
Adam cTor
Eve cTor
Son cTor


how does the order of the constructors decided?
all virtual bases are constructed first: that means Adam's G_d in the first case and Eve's G_d in the second case

then, the remaining bases are constructed in order of appearance in the class heads, in the first case:
Son needs to construct Adam first, then Eve, before it can be constructed. Adam needs nothing (virtual bases are already done), so it gets constructed. Eve needs another G_d (non-virtual base), so after Adam you get Eve's G_d, and then Eve, and then finally Son.

In the second case, Son needs Adam first, then Eve, Adam needs his G_d, and Eve needs nothing (virtuals are done), so the order is Adam's G_d, then Adam, then Eve, then Son

Last edited on
Yes - it follows from what Cubbi said (which is something I didn't know - thanks, Cubbi!) that, since Eve has a virtual base, that is created first. Then the process of creating Adam begins, which means that Adam's non-virtual base is created. Then Adam itself is created, then Eve (who doesn't need a new base, because the virtual one has already been created.

Then, once Adam and Eve are created, Son is created.
Curious to know the reason behind the omission of 'o' from God.
Curious to know the reason behind the omission of 'o' from God.

I can't speak for the OP, but I do know that in some religions (for example, Judaism), it's considered disrespectful to write out the complete name of the deity. In at least some of those religions, "God" is treated as the deity's name in English.

For people who feel this way, G-d is a common way to write it, although in C++ you can't use a hyphen/dash in the name of an entity, so G_d is the alternative.

This is really a topic for the Lounge, though.
Last edited on
if I have two (at least) virtuals which one does it start with? for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class G_d{
public:
    G_d() { cout<<" G_d cTor\n"; }
};
 
class Adam:  public G_d{
public:
        Adam() { cout<<"Adam cTor\n"; }
};
 
class Eve: virtual public G_d{
public:
        Eve() { cout<<"Eve cTor\n"; }
};
 
class Son: virtual public Adam, public Eve{
public:
        Son() { cout<<"Son cTor\n"; }
};
 
int main(){
        Son s;
        return 0;
}
Last edited on
Topic archived. No new replies allowed.