Module 03 - Marymota/CPP_Modules GitHub Wiki

Destruction of classes alignment

A derived class needs to access methods and attributes of the base class. A base class doesn't need to access elements of the derived class. Because of this, the derived object is always destroyed first.

THE DIAMOND PROBLEM

When using virtual inheritance, we are guaranteed to get only a single instance of the common base class. The DiamondTrap class will have only a single instance of the ClapTrap class, shared by both the ScavTrap and FragTrap classes.

Resources:
Solving the Diamond Problem with Virtual Inheritance


Virtual base class in C++

Virtual base classes are used in virtual inheritance in a way of preventing multiple "instances" of a given class appearing in an inheritance hierarchy when using multiple inheritances.

Need for Virtual Base Classes: In this exercise we have one class . This class in inherited by class and class . Both this classes ate inherited in a new class . Some data members/function of class are inherited twice to class causing ambiguity about which data/function member should be called. This confuses the compiler and displays an error.

To resolve this ambiguity when class is inherited in both class and class , it is declared as virtual base class. -> class FragTrap : virtual public ClapTrap -> class ScavTrap : virtual public ClapTrap A single copy of its data members is shared by all the base classes that use virtual base.

Resource:
Virtual base class in C++
Using-declaration