【C++缺省函数】 空类默认产生的6个类成员函数

目录(?)[+]

1、缺省构造函数。
2、缺省拷贝构造函数。
3、 缺省析构函数。
4、缺省赋值运算符。

5、缺省取址运算符。

6、 缺省取址运算符 const。


[cpp]  view plain  copy
 print ?
  1. <span style="font-size:18px;">  
[cpp]  view plain  copy
 print ?
  1. class A    
  2. {    
  3. public:    
  4.     A(){}//缺省构造函数    
  5.     
  6.     A(const A&){}//拷贝构造函数    
  7.     
  8.     ~A(){}//析构函数    
  9.     
  10.     A&operator=(const A&){}//赋值运算符    
  11.     
  12.     A*operator&(){}//取址运算符    
  13.     
  14.     const A*operator&()const{}//取址运算符 const    
  15. };  
[cpp]  view plain  copy
 print ?
  1. </span>  




       在C++中,编译器会为空类提供哪些默认成员函数?分别有什么样的功能呢? 


空类,声明时编译器不会生成任何成员函数

        对于空类,编译器不会生成任何的成员函数,只会生成1个字节的占位符。

        有时可能会以为编译器会为空类生成默认构造函数等,事实上是不会的,编译器只会在需要的时候生成6个成员函数:一个缺省的构造函数、一个拷贝构造函数、一个析构函数、一个赋值运算符、一对取址运算符和一个this指针。

 代码:

[cpp]  view plain  copy
 print ?
  1. <span style="font-size:18px;">  
[cpp]  view plain  copy
 print ?
  1. #include <iostream>    
  2. using namespace std;    
  3.     
  4. class Empty_one    
  5. {    
  6. };    
  7. class Empty_two    
  8. {};    
  9. class Empty_three    
  10. {    
  11.     virtual void fun() = 0;    
  12. };    
  13. class Empty_four :  public Empty_two, public Empty_three    
  14. {    
  15. };    
  16.     
  17. int main()    
  18. {    
  19.   cout<<"sizeof(Empty_one):"<<sizeof(Empty_one)<<endl;    
  20.   cout<<"sizeof(Empty_two):"<<sizeof(Empty_two)<<endl;    
  21.   cout<<"sizeof(Empty_three):"<<sizeof(Empty_three)<<endl;    
  22.   cout<<"sizeof(Empty_four):"<<sizeof(Empty_four)<<endl;    
  23.   return 0;    
  24. }  
[cpp]  view plain  copy
 print ?
  1. </span>  

结果:

分析:

类Empty_one、Empty_two是空类,但空类同样可以被实例化,而每个实例在内存中都有一个独一无二的地址,为了达到这个目的,编译器往往会给一个空类隐含的加一个字节,这样空类在实例化后在内存得到了独一无二的地址,所以sizeof(Empty_one)和sizeof(Empty_two)的大小为1。

类Empty_three里面因有一个纯虚函数,故有一个指向虚函数的指针(vptr),32位系统分配给指针的大小为4个字节,所以sizeof(Empty_three)的大小为4。

类Empty_four继承于Empty_two和Empty_three,编译器取消Empty_two的占位符,保留一虚函数表,故大小为4。

2、空类,定义时会生成6个成员函数

        当空类Empty_one定义一个对象时Empty_one pt;sizeof(pt)仍是为1,但编译器会生成6个成员函数:一个缺省的构造函数、一个拷贝构造函数、一个析构函数、一个赋值运算符、两个取址运算符。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class Empty  
  2. {};  
等价于:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class Empty  
  2. {  
  3.   public:  
  4.     Empty();                            //缺省构造函数  
  5.     Empty(const Empty &rhs);            //拷贝构造函数  
  6.     ~Empty();                           //析构函数   
  7.     Empty& operator=(const Empty &rhs); //赋值运算符  
  8.     Empty* operator&();                 //取址运算符  
  9.     const Empty* operator&() const;     //取址运算符(const版本)  
  10. };  
使用时的调用情况:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Empty *e = new Empty();    //缺省构造函数  
  2. delete e;                  //析构函数  
  3. Empty e1;                  //缺省构造函数                                 
  4. Empty e2(e1);              //拷贝构造函数  
  5. e2 = e1;                   //赋值运算符  
  6. Empty *pe1 = &e1;          //取址运算符(非const)  
  7. const Empty *pe2 = &e2;    //取址运算符(const)  

C++编译器对这些函数的实现:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. inline Empty::Empty()                          //缺省构造函数  
  2. {  
  3. }  
  4. inline Empty::~Empty()                         //析构函数  
  5. {  
  6. }  
  7. inline Empty *Empty::operator&()               //取址运算符(非const)  
  8. {  
  9.   return this;   
  10. }             
  11. inline const Empty *Empty::operator&() const    //取址运算符(const)  
  12. {  
  13.   return this;  
  14. }  
  15. inline Empty::Empty(const Empty &rhs)           //拷贝构造函数  
  16. {  
  17.   //对类的非静态数据成员进行以"成员为单位"逐一拷贝构造  
  18.   //固定类型的对象拷贝构造是从源对象到目标对象的"逐位"拷贝  
  19. }  
  20.   
  21. inline Empty& Empty::operator=(const Empty &rhs) //赋值运算符  
  22. {  
  23.   //对类的非静态数据成员进行以"成员为单位"逐一赋值  
  24.   //固定类型的对象赋值是从源对象到目标对象的"逐位"赋值。  
  25. }  
         例如:m是类C中的一个类型为T的非静态成员变量,若C没有声明拷贝构造函数(赋值运算符), m将会通过T的拷贝构造函数(赋值运算符)被拷贝构造(赋值);该规则递归应用到m的数据成员,直到找到一个拷贝构造函数(赋值运算符)或固定类型(例如:int、double、指针等)为止。

三、总结

上述运行结果依赖于编译器和64位、32位不同的系统。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值