J
Jess
Hello,
I've been reading Effective C++ about multiple inheritance, but I
still have a few questions. Can someone give me some help please?
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? 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? What
about the intermediate classes (those derived classes that aren't at
the end of the hierarchy)? 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?
Second, it is said that we should try to avoid putting data in a
virtual base class. If we use non-virtual inheritance then there will
be duplicated data members if the base class has data members.
However, once we declare virtual inheritance, then the compiler will
ensure no duplication, is this right? Therefore, it seems ok to me to
put data members into virtual base class. I guess the reason why we
should avoid putting data into a virtual base class is to make
compiler's life easier. In addition, the book also says if we don't
put data into virtual base class, then we "won't have to worry about
oddities in the initialization and assignment rules for such classes".
Could someone tell me what the author means exactly?
Third, if a class D privately inherits from class A and B, do we still
need to use virtual inheritance? If we use non-virtual inheritance,
would there be any data duplication?
Finally, I'm still not sure when multiple inheritance should be used,
any insight will be highly appreciated!
Thanks!
Jess
I've been reading Effective C++ about multiple inheritance, but I
still have a few questions. Can someone give me some help please?
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? 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? What
about the intermediate classes (those derived classes that aren't at
the end of the hierarchy)? 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?
Second, it is said that we should try to avoid putting data in a
virtual base class. If we use non-virtual inheritance then there will
be duplicated data members if the base class has data members.
However, once we declare virtual inheritance, then the compiler will
ensure no duplication, is this right? Therefore, it seems ok to me to
put data members into virtual base class. I guess the reason why we
should avoid putting data into a virtual base class is to make
compiler's life easier. In addition, the book also says if we don't
put data into virtual base class, then we "won't have to worry about
oddities in the initialization and assignment rules for such classes".
Could someone tell me what the author means exactly?
Third, if a class D privately inherits from class A and B, do we still
need to use virtual inheritance? If we use non-virtual inheritance,
would there be any data duplication?
Finally, I'm still not sure when multiple inheritance should be used,
any insight will be highly appreciated!
Thanks!
Jess