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

What is the difference between structure and class?

820


How can I learn c++ easily?

849


Why #include is used?

817


Why is polymorphism useful?

807


Can non graphic characters be used and processed in C++?

926


What is an adaptor class in c++?

799


What is a multiset c++?

793


What is the difference between multiple and multilevel inheritance in c++?

893


What is the use of main function in c++?

752


Why c++ is not a pure oop language?

753


Explain the uses of static class data?

855


What is the use of string in c++?

764


Describe Trees using C++ with an example.

857


What do you mean by delegate? Can a user retain delegates?

734


What is c++ & why it is used?

829