空类在声明时不会生成任何成员函数,只会生成1个字节的占位符。
当定义了空类的对象时,编译器会生成6个成员函数:缺省的构造函数、拷贝构造函数、赋值运算符、析构函数、两个取地址运算符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> using namespace std;
class A { public: A(){}; A(const A &tmp){}; ~A(){}; A &operator=(const A &tmp){}; A *operator&() { return this; }; const A *operator&() const { return this; }; };
int main() { A *p = new A(); cout << "sizeof(A):" << sizeof(A) << endl; delete p; return 0; }
|
作者:力扣 (LeetCode)
链接:https://leetcode.cn/leetbook/read/cpp-interview-highlights/eflvg3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。