Re: some questions about multiple inheritance
Jess wrote:
Thanks a lot!
On Jun 26, 11:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Jess wrote:
I've been reading Effective C++ about multiple inheritance, but I
still have a few questions. Can someone give me some help please?
We can try...
First, it is said that if virtual inheritance is used, then "the
responsibility for initializing a virtual base is borne by the most
derived class in the hierarchy". What does it mean?
It means that the compiler generates code such that the virtual base
class' constructor is invoked *not* by the constructor of the class
that directly inherits the virtual base but the most derived one.
Initializing
base class is usually done automatically by the compiler, but a
derived class can invoke the base class' constructor. What special
initialization work has to be done by the most derived class?
Here is an example:
struct vbase {
int i;
vbase(int ii) : i(ii) {}
};
struct a : virtual vbase {
a() : vbase(1) {}
};
struct b : virtual vbase {
b() : vbase(2) {}
};
struct mostderived : a, b {
mostderived() : vbase(42) {} // note 'vbase' initialisation
};
#include <iostream>
int main() {
mostderived m;
std::cout << m.i << std::endl;
}
What do you expect the program to output? The default c-tor of 'a'
makes the 'i' member of 'vbase' 1, 'b' makes it 2. But when the 'm'
object is created in the 'main' function, the 'i' member will be
initialised to 42 because the most derived class is responsible for
initialising the virtual base class.
What
about the intermediate classes (those derived classes that aren't at
the end of the hierarchy)?
What about them? Do you expect to instantiate them independently?
In your example, if "mostderived" wants to init "a" and "b" explicitly
by calling their constructors, I think "mostderived" can do so, right?
Yes, any class deriving from another class initialises the base class
subobject in the constructor initialiser list.
If so, would "a" and "b"'s constructors iniit "vbase" by calling
vbase's constructor?
No, they would not. That's the trick. The virtual base class object
(subobject) is initialised by the most derived class.
Do they need to do anything to init the
virtual base class and does the most derived class need to do
anything to init the intermediate classes?
Yes, in case you _do_ instantiate them.
I think you mean I can call "a" and "b"'s constructors?
No, I don't mean you can call them. I mean if you define your own
separate, stand-alone, objects of type 'a' and/or 'b', then *their*
'vbase' subobjects would be initialised in the respective c-tors.
a aa; // aa.vbase::i is 1
b bb; // bb.vbase::i is 2
mostderived m; // m.vbase::i is NEITHER 1 NOR 2
[..]
Third, if a class D privately inherits from class A and B, do we
still need to use virtual inheritance?
To accomplish what?
To inherit the implemenations.
Uh... Right. Access specifiers have no effect on who has to init
what virtual base class.
If we use non-virtual inheritance,
would there be any data duplication?
Data duplication of what?
If A and B derive from one base class (vbase) and vbase has data
members. Without inheriting virtually, it seems D can still get
duplicated data.
Yes. I thought you were talking of something else.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask