AFAIK, Virtual inheritance solves the diamond problem but what if I use virtual to simply inherit from base class? Whats the difference with using virtual here?
class A
{
/* ... */
};
class B : virtual public A
{
/* ... */
};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a few differences. The object layout will be different (in the case of virtual inheritance there will be an extra hidden pointer to the base in the derived subobject), initialization of the subobjects will differ (a type deriving from
Bwill have to call theAconstructor directly in the initialization list). Any code inBmember functions that refers toAmembers (data or functions) will require an extra indirection (through the pointer mentioned before), and possibly other differences.Virtual inheritance is a complex matter and you should probably read more of the links provided in the answers to the previous question of yours than try to understand it by just getting bits and pieces from separate questions.
Hint: If you want to understand the details of how virtual inheritance is handled by the compiler, you should focus on the memory layout of the objects. Once you understand why the extra pointer per class that virtually derives is needed, the rest is simple.