类大小
1.空类占用一个字节。若父类为空类,创建子类对象时,父类不占用子类空间。
#include<iostream>
using namespace std;
class Father{
public:
Father(){}
~Father(){}
};
class Son:public Father{
public:
Son(){}
~Son(){}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
运行结果:
#include<iostream>
using namespace std;
class Father{
public:
Father(){}
~Father(){}
};
class Son:public Father{
public:
Son(){}
~Son(){}
int a;
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
运行结果:
2.构造函数和析构函数不占用类空间
#include<iostream>
using namespace std;
class Father{
};
class Son:public Father{
public:
Son(){}
~Son(){}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
执行结果:
3.普通成员函数不占用类空间:
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
// void test(){int a=5;}
};
class Son:public Father{
public:
Son(){}
~Son(){}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
执行结果:
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
void test(){int a=5;}
};
class Son:public Father{
public:
Son(){}
~Son(){}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
4.一个类中只要有虚成员函数,且无论有多少个虚成员函数,都会开辟一块内存来存放虚函数表指针,这个指针的大小等于计算机的位数/1字节位数。
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
virtual void test(){int a=5;}
};
class Son:public Father{
public:
Son(){}
~Son(){}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
virtual void test(){int a=5;}
virtual void test01(){int b=6;}
};
class Son:public Father{
public:
Son(){}
~Son(){}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
5.子类和父类共用虚函数表指针。
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
virtual void test(){int a=5;}
virtual void test01(){int b=6;}
};
class Son:public Father{
public:
Son(){}
~Son(){}
void test(){int a=6;}
void test01(){int b=7;}
virtual void test02(){int c=5;}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
6.即使父类是空类,只要将析构函数声明为虚函数,就会产生一个虚函数表指针。
#include<iostream>
using namespace std;
class Father{
public:
Father(){}
virtual ~Father(){}
};
class Son:public Father{
public:
Son(){}
~Son(){}
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
7.类也会遵循结构体中字节对齐的规则。
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
void test(){int a=5;}
};
class Son:public Father{
public:
Son(){}
~Son(){}
char c;//
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
void test(){int a=5;}
};
class Son:public Father{
public:
Son(){}
~Son(){}
int c;//
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}
8.静态成员不占用类的空间大小。
#include<iostream>
using namespace std;
class Father{
public:
int a;
char b;
void test(){int a=5;}
static int g;
};
class Son:public Father{
public:
Son(){}
~Son(){}
int c;
};
int main()
{
Father f;
Son s;
cout<<"sizeof(f):"<<sizeof(f)<<endl;
cout<<"sizeof(s):"<<sizeof(s)<<endl;
return 0;
}