EBO, empty base optimization, which is the optimization of the blank base class, can make the derived class space-saving;
But if you use a composite (composition) method, because the new object is defined, the compiler fills (padding) and fills in an int, 4-bit space;
Code:
/*************************************************
File:test.cpp
copyright:c.l.wang
author:c.l. Wang
date:2014-04-11
description:test
email:morndragon@126.com
******************************* /
/*eclipse CDT, gcc 4.8.1*/
#include <iostream>
using namespace std;
Class Empty {};
Class Holdsanint {
private:
int x;
Empty e;
};
Class Holdsanintp:private Empty {
private:
int x;
};
int main () {
std::cout << "int:" << sizeof (int) << Std::endl;
Std::cout << "Holdsanint (Class):" << sizeof (holdsanint) << Std::endl;
Std::cout << "HOLDSANINTP (Inheritance):" << sizeof (HOLDSANINTP) << Std::endl;
return 0;
}
Output:
Int:4
Holdsanint (Class): 8
holdsanintp (inheritance): 4
Author: csdn Blog spike_king
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/