关于类中类型转换

本文探讨了C++中如何通过显式和隐式类型转换来避免潜在的问题,并提供了具体的代码示例,包括使用explicit关键字和proxy class的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[cpp]  view plain copy
  1. #include <iostream>   
  2. using namespace std;   
  3. // example1:单参数构造函数可将内建类型转换为自建的类类型  
  4. class A  
  5. {  
  6. public:  
  7.     A(double i=0.0){d=i;}  
  8.     void print()  
  9.     {  
  10.         cout<<"d="<<d<<endl;  
  11.     }  
  12.     ~A(){cout<<d<<"析构"<<endl;}  
  13. protected:  
  14. private:  
  15.     double d;  
  16. };  
  17. // example2:operator 目的类型(){}可见类对象隐式转换为指定的类型  
  18. class B  
  19. {  
  20. public:  
  21.     B(double i){d=i;}  
  22.     operator double()  
  23.     {  
  24.         return d;  
  25.     }  
  26.     ~B(){cout<<d<<"析构"<<endl;}  
  27. protected:  
  28. private:  
  29.     double d;  
  30. };  
  31. void main()  
  32. {  
  33.     A a;  
  34.     a=3.693;  
  35.     a.print();  
  36.     B b(9.23);  
  37.     cout<<b<<endl;  
  38. }  


输出的结果:

3.693析构(3.693先生成一个A的临时对象,转换后对象销毁)

d=3.693

9.23

9.23析构

3.693析构(堆栈式销毁:后进先出)

 

《More Effective C++ 》中认为要避免这样的隐式类型转换:分别有对应的解决方法,如下

[cpp]  view plain copy
  1. #include <iostream>   
  2. using namespace std;   
  3. // example1:单参数构造函数可将内建类型转换为自建的类类型  
  4. class A  
  5. {  
  6. public:  
  7.     explicit A(double i=0.0){d=i;}//使用explicit解决隐式类型转换的问题  
  8.     void print()  
  9.     {  
  10.         cout<<"d="<<d<<endl;  
  11.     }  
  12.     ~A(){cout<<d<<"析构"<<endl;}  
  13. protected:  
  14. private:  
  15.     double d;  
  16. };  
  17. // example2:operator 目的类型(){}可见类对象隐式转换为指定的类型  
  18. class B  
  19. {  
  20. public:  
  21.     B(double i){d=i;}  
  22.     double asDouble() //避免类型转换的方法  
  23.     {  
  24.         return d;  
  25.     }  
  26.     ~B(){cout<<d<<"析构"<<endl;}  
  27. protected:  
  28. private:  
  29.     double d;  
  30. };  
  31. void main()  
  32. {  
  33.     A a;  
  34. //  a=3.693;    //it's wrong  
  35.     a=static_cast<A>(3.693);//强制类型转换  
  36.     a.print();  
  37.     B b(9.23);  
  38. //  cout<<b<<endl;//it's wrong  
  39.     cout<<b.asDouble()<<endl;  
  40.   
  41. }  


 以下是proxy class的用法,也能避免隐式的类型转换

[cpp]  view plain copy
  1. #include <iostream>   
  2. using namespace std;   
  3. // example1:单参数构造函数可将内建类型转换为自建的类类型  
  4. // 方法1:使用explicit  
  5. // 方法2:使用proxy class  
  6. //这里演示方法2  
  7. class A  
  8. {  
  9. public:  
  10.     class Avalue  
  11.     {  
  12.     public:  
  13.         Avalue(double d1=0){dd=d1;};  
  14.         double value(){return dd;};  
  15.     private:  
  16.         double dd;  
  17.     };  
  18.     A(Avalue aobj){d=aobj.value();}  
  19.     void print()  
  20.     {  
  21.         cout<<"d="<<d<<endl;  
  22.     }  
  23.     ~A(){cout<<d<<"析构"<<endl;}  
  24. protected:  
  25. private:  
  26.     double d;  
  27.   
  28. };  
  29.   
  30. void main()  
  31. {  
  32.     A a(2.4);//这是可行  
  33.     a=2.4;   //it's wrong。  
  34.     a.print();  
  35.   
  36.   
  37. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值