C++空类详解
C++空类详解
发布时间:2016-12-28 来源:查字典编辑
摘要:空类默认产生的成员:classEmpty{};Empty();//默认构造函数Empty(constEmpty&);//默认拷贝构造函数~E...

空类默认产生的成员:

class Empty {};

Empty(); // 默认构造函数

Empty( const Empty& ); // 默认拷贝构造函数

~Empty(); // 默认析构函数

Empty& operator=( const Empty& ); // 默认赋值运算符

Empty* operator&(); // 取址运算符

const Empty* operator&() const; // 取址运算符 const

给出一个例子:

复制代码 代码如下:

#include<iostream>

using namespace std;

class Empty

{

public:

Empty *operator&()

{

cout<<"AAAA"<<endl;

return this;

}

const Empty* operator&() const

{

cout<<"BBBB"<<endl;

return this;

}

};

int main(void)

{

Empty e;

Empty *p=&e;

const Empty e2;

const Empty *p2=&e2;

cout<<sizeof(Empty)<<endl;

}

运行结果:

C++空类详解1

可见:

Empty *p=&e调用了Empty* operator&()运算符函数

const Empty *p2=&e2调用了const Empty* operator&() const运算符函数。

空类的大小为1字节。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C语言学习
热门C语言学习
编程开发子分类