源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C++空类详解

  • 时间:2021-03-22 07:25 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C++空类详解
[b]空类默认产生的成员: [/b]class Empty {}; Empty(); // 默认构造函数 Empty( const Empty& ); // 默认拷贝构造函数 ~Empty(); // 默认析构函数 Empty& operator=( const Empty& );  // 默认赋值运算符 Empty* operator&();               // 取址运算符 const Empty* operator&() const;    // 取址运算符 const [b]给出一个例子: [/b]
[u]复制代码[/u] 代码如下:
#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; }
运行结果:
[img]http://files.jb51.net/file_images/article/201309/201309120906595.jpg[/img] [b]可见: [/b]Empty *p=&e调用了Empty* operator&()运算符函数 const Empty *p2=&e2调用了const Empty* operator&() const运算符函数。 空类的大小为1字节。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部