💡 TL;DR:/ @file blackclass.cpp @brief The size of an empty class is 1 byte @author 光城 @version v1 @date 2019-07-21 / using namespace std; class A{}; int main cout<<sizeofA<<endl; r
Last updated:2026-10-13
C++ sizeof Explained: Type Sizes and the Empty-Class 1¶
First a summary, then real examples below — hands-on time!
- The size of an empty class is 1 byte
- Within a class, the virtual functions themselves, member functions (both static and non-static), and static data members take up no storage space in class objects.
- For a class containing virtual functions, no matter how many virtual functions there are, there is only one virtual pointer — the size of the vptr.
- With ordinary inheritance, the derived class inherits all the base class's functions and members, and the size must be computed according to byte alignment
- With virtual-function inheritance, whether single or multiple inheritance, the base class's vptr is inherited. (4 bytes on a 32-bit OS, 8 bytes on a 64-bit OS)!
- With virtual inheritance, the base class's vptr is inherited. Note that the result is compiler-dependent: GCC merges the virtual base pointer with the virtual function table pointer (e.g. 8/8/16), while MSVC/Windows stores them separately (e.g. 8/8/32); see issue #246.
1. Principle 1¶
/**
* @file blackclass.cpp
* @brief 空类的大小为1字节
* @author 光城
* @version v1
* @date 2019-07-21
*/
#include<iostream>
using namespace std;
class A{};
int main()
{
cout<<sizeof(A)<<endl;
return 0;
}
2. Principle 2¶
/**
* @file static.cpp
* @brief 静态数据成员
* 静态数据成员被编译器放在程序的一个global data members中,它是类的一个数据成员,但不影响类的大小。不管这个类产生了多少个实例,还是派生了多少新的类,静态数据成员只有一个实例。静态数据成员,一旦被声明,就已经存在。
* @author 光城
* @version v1
* @date 2019-07-21
*/
#include<iostream>
using namespace std;
class A
{
public:
char b;
virtual void fun() {};
static int c;
static int d;
static int f;
};
int main()
{
/**
* @brief 16 字节对齐、静态变量不影响类的大小、vptr指针=8
*/
cout<<sizeof(A)<<endl;
return 0;
}
3. Principle 3¶
/**
* @file morevir.cpp
* @brief 对于包含虚函数的类,不管有多少个虚函数,只有一个虚指针,vptr的大小。
* @author 光城
* @version v1
* @date 2019-07-21
*/
#include<iostream>
using namespace std;
class A{
virtual void fun();
virtual void fun1();
virtual void fun2();
virtual void fun3();
};
int main()
{
cout<<sizeof(A)<<endl; // 8
return 0;
}
4. Principles 4 and 5¶
/**
* @file geninhe.cpp
* @brief 1.普通单继承,继承就是基类+派生类自身的大小(注意字节对齐)
* 注意:类的数据成员按其声明顺序加入内存,无访问权限无关,只看声明顺序。
* 2.虚单继承,派生类继承基类vptr
* @author 光城
* @version v1
* @date 2019-07-21
*/
#include<iostream>
using namespace std;
class A
{
public:
char a;
int b;
};
/**
* @brief 此时B按照顺序:
* char a
* int b
* short a
* long b
* 根据字节对齐4+4+8+8=24
*
* 或编译器优化
* char a
* short a
* int b
* long b
* 根据字节对齐2+2+4+8=16
*/
class B:A
{
public:
short a;
long b;
};
/**
* 把A的成员拆开看,char为1,int为4,所以是1+(3)+4+1+(3)=12,()为字节补齐
*/
class C
{
A a;
char c;
};
class A1
{
virtual void fun(){}
};
class C1:public A1
{
};
int main()
{
cout<<sizeof(A)<<endl; // 8
cout<<sizeof(B)<<endl; // 16 或 24
cout<<sizeof(C)<<endl; // 12
/**
* @brief 对于虚单函数继承,派生类也继承了基类的vptr,所以是8字节
*/
cout<<sizeof(C1)<<endl; // 8
return 0;
}
5. Principle 6¶
/**
* @file virnhe.cpp
* @brief 虚继承
* @author 光城
* @version v1
* @date 2019-07-21
*/
#include<iostream>
using namespace std;
class A
{
virtual void fun() {}
};
class B
{
virtual void fun2() {}
};
class C : virtual public A, virtual public B
{
public:
virtual void fun3() {}
};
int main()
{
/**
* @brief 8 8 16 派生类虚继承多个虚函数,会继承所有虚函数的vptr
*/
cout<<sizeof(A)<<" "<<sizeof(B)<<" "<<sizeof(C);
return 0;
}
FAQ¶
Why is sizeof an empty class 1?¶
C++ requires distinct objects to have distinct addresses; a size of 0 would make array elements overlap, so an empty class occupies at least 1 byte.
How much does a virtual function add to sizeof?¶
One vptr: 8 bytes on 64-bit platforms. Multiple inheritance can introduce several vptrs and a larger size.
Is sizeof evaluated at compile time or runtime?¶
Compile time (always, in C++). The result is a constant expression usable as an array bound or template argument.
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Explained: Constants, const Pointer vs Pointer to const | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事