In C what is a virtual base class

0 votes

I'm curious what a "virtual base class" is and what it signifies.

Let me provide an example.

class Foo
{
public:
    void DoSomething() { /* ... */ }
};

class Bar : public virtual Foo
{
public:
    void DoSpecific() { /* ... */ }
};
Jun 9 in C++ by Nicholas
• 5,020 points
24 views

1 answer to this question.

0 votes

When employing multiple inheritance, virtual base classes are used to prevent several "instances" of a particular class from appearing in an inheritance hierarchy.

Consider the following example:

class A { public: void Foo() {} };
class B : public A {};
class C : public A {};
class D : public B, public C {};

The preceding class hierarchy yields the "dreaded diamond," which looks like this:

  A
 / \
B   C
 \ /
  D

A D instance will consist of B, which contains A, and C, which also includes A. 

So you have two "instances" of A (for lack of a better term).

There is the possibility of ambiguity in this circumstance. 

This is what happens when you do this:

D d;
d.Foo(); // is this B's Foo() or C's Foo() ??

Virtual inheritance exists to address this issue. 

While you use the virtual keyword when inheriting a class, you're informing the compiler that you only want one instance.

class A { public: void Foo() {} };
class B : public virtual A {};
class C : public virtual A {};
class D : public B, public C {};

This signifies that the hierarchy has just one "instance" of A. 

Hence

D d;
d.Foo(); // no longer ambiguous

answered Jun 10 by Damon
• 4,960 points

Related Questions In C++

0 votes
1 answer

What is a lambda expression in C++11?

In C++11, what is a lambda expression? A: It's the object of an autogenerated class with overloading operator() const under the hood.  Closure is a type of object that is produced by the compiler.  This 'closure' idea is similar to C++11's bind notion.  Lambdas, on the other hand, usually produce better code.  Full inlining is also possible with calls through closures. Q: When do you think I'd utilise one? A: Define "simple and tiny logic" and request that the compiler generate the code from the preceding question.  You tell the compiler the expressions you wish to be inside the operator ().  The compiler will produce everything else for you. Q: What kind of problem do they tackle that couldn't be solved before they were introduced? A: It's some form of syntactic sugar, like using operators instead of functions for custom add, subtract, and other operations... However, wrapping 1-3 lines of genuine logic to some classes, and so on, saves additional lines of needless code!  Some engineers believe that if the number of lines is reduced, there is a lower likelihood of mistakes (which I agree with). Example of usage auto x = [=](int arg1){printf("%i", ...READ MORE

answered Jun 15 in C++ by Damon
• 4,960 points
26 views
0 votes
0 answers

What is the fastest way to transpose a matrix in C++?

I have a reasonably large matrix that I need to transpose.  Assume, for example, that my matrix is a b c d e f g h ...READ MORE

Jul 15 in C++ by Nicholas
• 5,020 points
10 views
0 votes
0 answers

What type of members can I add in a c++ abstract class

Hello, let's say I have an abstract class with a few pure abstract functions and a few classes that derive from it, and all of the data from these classes eventually becomes similar, I was wondering if it would be wise, or even possible, to declare a vector under protected in the abstract class to collect the data so something like that. class A { protected: vector <string> ...READ MORE

Jul 27 in C++ by Nicholas
• 5,020 points
8 views
0 votes
0 answers

What is a stream in C++?

I've been hearing a lot about streams, ...READ MORE

2 days ago in C++ by Nicholas
• 5,020 points
7 views
0 votes
1 answer
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
206 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,414 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
808 views
0 votes
1 answer

What is a Class and Object in C++?

A Class is like a blueprint, an ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
21 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 4,960 points
45 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP