What is Virtual Inheritance?

Answer Posted / achal ubbott

The concept of virtual inheritance was evolved to avoid
ambiguity/duplication.

e.g.
class base
{
int value;
};
now we do some multiple inheritance
class A:public base {};
class B:public base {};

Now value is member to both the classes A and B.
Lets have a class C that inherits from both A and B.
class C:public A, public B {};

Now should that mean that C have 2 copies of value as its
data member? Yes and this leads to ambiguity.
So do like this

class C:virtual public A,virtual public B {};

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain what are mutator methods in c++?

570


When one must use recursion function? Mention what happens when recursion functions are declared inline?

646


Why isn't sizeof for a struct equal to the sum of sizeof of each member?

540


What are shallow and deep copies?

629


Which of the following is evaluated first: a) && b) || c) !

1803






What is static class data?

573


Do vectors start at 0 c++?

532


What is c++ & why it is used?

588


How is c++ used in the real world?

569


Why do we use constructor?

595


What is difference between c++ and c ++ 14?

572


Explain the difference between using macro and inline functions?

574


Explain selection sorting. Also write an example.

580


How can we access protected and private members of a class?

589


Why do we use setw in c++?

641