dimanche 29 septembre 2019

Why compiler takes extra bytes during virtual inheritance? [duplicate]

This question already has an answer here:

While I was learning about virtual inheritance in C++. In the following program, the size of the derived class is 16 extra bytes than that of the base class, however this amount varies for different data types used in base class. How compiler uses those extra bytes? How is this behaviour defined in C++?

#include<iostream>

using namespace std; 

class base {
    double arr[10];  
}; 

class b1: virtual public base { }; 

class b2: virtual public base { }; 

class derived: public b1, public b2 {}; 

int main(void) 
{ 
    cout<<sizeof(derived)<<" "<<sizeof(base); 
    return 0; 
} 

Output:

96 80

Aucun commentaire:

Enregistrer un commentaire