Hello all, at start I want to tell that I cannot find anywhere an answer, probably because (in my opinion also) everything should work ok, so the problem is - I have 3 classes:
My question is - Why when I build MainClass like this: MainClass ms(5);
starts at first default (SharedClass()) constructor of SharedClass, not SharedClass(constint &a) constructor??? (after that, program runs correctly: InheritedClass(constint &a) & MainClass(constint &a))
If anybody knows why this construction don't work as I expected please tell my (or any advice). When I run a SharedClass constructor with parameter under constructor of MainClass, everything work fine, but is it correct? I think that, I don't have to do like this (or even I shouldn't do this).
Ok, I found the answer:
If you have OneClass which inherit AnotherClass by virtual inheritance, and then you inherit this OneClass by SecondClass, then obligation to creation AnotherClass goes to this SecondClass. It looks inconvenient, but it has sense.
Eg.
You have SharedClass, AClass, BClass, MainClass.
AClass & BClass are virtually inherited by a SharedClass and MainClass inherit AClass & BClass.
Then you have only one instance of SharedClass in this hierarchy, and this is ambiguous which construction of SharedClass is right (from AClass or from BClass), so creation goes to MainClass.
I hope that I'm understandable, and please, if you found that I'm wrong, make a comment.