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 are the stages in the development cycle?

577


How can you specify a class in C++?

802


How many namespaces are there in c++?

560


What is the header file for setw?

579


What is static class data?

577






What are c++ files?

582


Explain differences between alloc() and free()?

580


Why we use #include iostream in c++?

585


What is #include cstdlib in c++?

664


What is a stack c++?

576


C is to C++ as 1 is to a) What the heck b) 2 c) 10

641


Can we use struct in c++?

597


How does code-bloating occur in c++?

753


Can you explicitly call a destructor on a local variable?

602


Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321].

963