cpp虚继承中的类的内存占用为多少?
# include<iostream>
using namespace std;
class Base
{
private:
/* data */
public:
// Base(){
// m_Age = 0;
// cout << "Base" << endl;
// };
//~Base();
int m_Age;
};
class Son1:virtual public Base
{
private:
/* data */
public:
// Son1(){
// m_Age = 10;
// cout << "Son1" << endl;
// };
//~Son1();
};
class Son2:virtual public Base
{
private:
/* data */
public:
// Son2(/* args */){
// m_Age = 20;
// cout << "Son2" << endl;
// };
//~Son2();
};
class Grandson:public Son1,public Son2
{
private:
/* data */
public:
// Grandson(/* args */){
// m_Age = 30;
// cout << "Grandson" << endl;
// };
//~Grandson();
};
int main(){
Grandson g1;
g1.m_Age = 100;
//virtual后所有共享同一地址的属性,用虚指针指向(偏移量判断)
cout << g1.m_Age << endl;
cout << sizeof(Son1) << endl;
cout << sizeof(Son2) << endl;
cout << sizeof(Base) << endl;
cout << sizeof(Grandson) << endl;
}
结果如下:(不太明白为什么)