C++ 中空类的对象大小是多少?
以下是一个示例代码,用于查找空类的对象大小。
实例
#include <bits/stdc++.h>
using namespace std;
class p1 {
public:
void first() {
cout << "\nThe parent class p1 function is called.";
}
};
class p2
{ };
int main() {
cout << "The size of non-empty class p1 = " << sizeof(p1);
cout << "\nThe size of empty class p2 = " << sizeof(p2);
p2 p;
cout << "\nThe size of object of empty class p2 = " << sizeof(p);
p1 o;
cout << "\nThe size of object of non-empty class p1 = " << sizeof(o);
return 0;
}输出
The size of non-empty class p1 = 1 The size of empty class p2 = 1 The size of object of empty class p2 = 1 The size of object of non-empty class p1 = 1
在以上程序中,创建了一个空类 p2。
class p2
{ };类和对象的大小打印如下 -
cout << "The size of non-empty class p1 : " << sizeof(p1); cout << "\nThe size of empty class p2 : " << sizeof(p2); p2 p; cout << "\nThe size of object of empty class : " << sizeof(p); p1 o; cout << "\nThe size of object of non-empty class p1 : " << sizeof(o);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP