# Virtual Base Classes

> A virtual base class in C++ is a base class inherited with the virtual keyword so that only one shared copy of its members exists in the final object, no matter how many inheritance paths lead to it. It solves the diamond problem, where a class inheriting from two classes with a common ancestor would otherwise hold two ambiguous copies.

Canonical: https://devsunite.com/resources/core-subjects/oop/virtual-base-classes
Last updated: 2026-11-22

Subject: OOP · Category: Inheritance · 3 min read

When a class inherits from two classes that both share a common ancestor, C++ would normally create two separate copies of that shared ancestor's members inside the final object, one through each path. This ambiguous duplication is called the diamond problem, and virtual base classes are C++'s fix for it.

## How the diamond problem happens

Picture this hierarchy:

```
        Animal
       /      \
     Bird      Mammal
       \      /
        Bat
```

`Bat` inherits from both `Bird` and `Mammal`, and both of those inherit from `Animal`. Without virtual inheritance, `Bat` would end up containing two separate, independent copies of everything `Animal` defined, one inherited via `Bird`, one inherited via `Mammal`. Any attempt to access an `Animal` member through a `Bat` object becomes ambiguous: which copy do you mean?

## How virtual base classes fix it

Declaring the inheritance from `Animal` as `virtual` in both `Bird` and `Mammal` tells the compiler to share a single instance of `Animal` across the entire hierarchy, no matter how many paths lead to it.

```cpp
#include <iostream>
using namespace std;

class Animal {
public:
    void breathe() { cout << "Breathing..." << endl; }
};

class Bird : virtual public Animal {};
class Mammal : virtual public Animal {};

class Bat : public Bird, public Mammal {};

int main() {
    Bat b;
    b.breathe();  // no ambiguity, only one Animal exists in the object
    return 0;
}
```

Without the `virtual` keyword on both `Bird` and `Mammal`'s inheritance from `Animal`, `b.breathe()` would fail to compile with an ambiguity error, the compiler wouldn't know which of the two `Animal` copies to use.

## Where this matters in real systems

The diamond problem is exactly why languages designed after C++, like Java and C#, chose to disallow multiple inheritance of classes entirely and only allow multiple inheritance of interfaces (which carry no implementation state to duplicate, so there's nothing to be ambiguous about). Codebases that do use C++'s multiple inheritance for genuine reasons, mixins for shared cross-cutting behavior, for instance, almost always reach for virtual inheritance the moment there's any chance of a shared ancestor appearing through more than one path.

## What interviewers actually ask about virtual base classes

- **"What is the diamond problem?"** When a class inherits the same base class through two or more separate paths, creating duplicate, ambiguous copies of that base class's members.
- **"How do you fix the diamond problem in C++?"** Declare the inheritance from the shared ancestor as `virtual` in the intermediate classes, so only one shared instance exists.
- **"Why doesn't Java have this problem?"** Java doesn't allow multiple inheritance of classes at all, only of interfaces, and interfaces have no state to duplicate, so there's nothing to be ambiguous about.
- **"Does virtual inheritance have any cost?"** Yes, objects using virtual inheritance typically carry extra overhead (a pointer to a shared virtual base table) and construction order becomes more complex, since the virtual base is constructed once by the most-derived class, not by the intermediate classes.

## Key takeaways

- The diamond problem happens when a class inherits the same base class through multiple separate paths.
- It causes ambiguous, duplicated copies of the shared base class's members.
- Virtual base classes fix this by ensuring only one shared instance of the common ancestor exists.
- Languages like Java sidestep the whole problem by disallowing multiple class inheritance entirely.

## Related

- [Previous: Constructor and Destructor Order in Inheritance](https://devsunite.com/resources/core-subjects/oop/constructor-and-destructor-in-inheritance)
- [Next: Composition vs Aggregation vs Inheritance](https://devsunite.com/resources/core-subjects/oop/composition-vs-aggregation-vs-inheritance)
- [More in Inheritance: Overview of Inheritance](https://devsunite.com/resources/core-subjects/oop/overview-of-inheritance)
- [More in Inheritance: Super and Sub Classes](https://devsunite.com/resources/core-subjects/oop/super-and-sub-classes)
- [More in Inheritance: Types of Inheritance](https://devsunite.com/resources/core-subjects/oop/types-of-inheritance)
- [More in Inheritance: Access Specifiers in Inheritance](https://devsunite.com/resources/core-subjects/oop/access-specifiers-in-inheritance)
- [All 72 OOP interview questions](https://devsunite.com/resources/core-subjects/oop)
