Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual inheritance using empty classes

Tags:

c++

Can anyone tell the exact reason for the output of the following code in C++ ?The output that I received for the code is included in header comments. What does it have to do with virtual table and v pointer.

/* sizeof(Empty) 1                                                                                
 sizeof(Derived1) 1 
 sizeof(Derived2) 8 
 sizeof(Derived3) 1 
 sizeof(Derived4) 16 
 sizeof(Dummy) 1
*/

#include <iostream>
using namespace std;

class Empty
{};

class Derived1 : public Empty
{};

class Derived2 : virtual public Empty
{};

class Derived3 : public Empty
{    
char c;
};

class Derived4 : virtual public Empty
{
char c;
};

class Dummy
{
 char c;
};

int main()
{

    cout << "sizeof(Empty) " << sizeof(Empty) << endl;
    cout << "sizeof(Derived1) " << sizeof(Derived1) << endl;
    cout << "sizeof(Derived2) " << sizeof(Derived2) << endl;
    cout << "sizeof(Derived3) " << sizeof(Derived3) << endl;
    cout << "sizeof(Derived4) " << sizeof(Derived4) << endl;    
    cout << "sizeof(Dummy) " << sizeof(Dummy) << endl;
    return 0;

}
like image 237
user1543544 Avatar asked Jun 25 '15 13:06

user1543544


People also ask

Can virtual class be inherited?

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.

What is virtual inheritance with example?

Virtual inheritance is used when we are dealing with multiple inheritance but want to prevent multiple instances of same class appearing in inheritance hierarchy. From above example we can see that “A” is inherited two times in D means an object of class “D” will contain two attributes of “a” (D::C::a and D::B::a).

What happens if we don't use virtual function in inheritance?

If you don't use virtual functions, you don't understand OOP yet. Because the virtual function is intimately bound with the concept of type, and type is at the core of object-oriented programming, there is no analog to the virtual function in a traditional procedural language.

How a virtual base class helps in inheritance?

Virtual base classes in C++ are used to prevent multiple instances of a given class from appearing in an inheritance hierarchy when using multiple inheritances.


1 Answers

Firstly, even a class with no members must have a non-zero size. The standard insists on that. Otherwise pointer arithmetic and arrays would not work as an array of a zero-sized class would have all its elements in the same place!

The fact that the other sizes differ may well be due to a v-table. But that is not mandated explicitly in the standard, so is a manifestation of the way your compiler is dealing with things.

Note also that polymorphism requires at least one virtual method to be defined in a base class. This accounts for sizeof(Derived1) being the same size as the base class.

like image 157
Bathsheba Avatar answered Sep 20 '22 02:09

Bathsheba