类的转换函数

转换函数的特点:

(1)必须是类方法。

(2)没有返回值。

(3)没有参数。

(4)只能转换为内置类型,不可以转换为自定义类型。

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
using  std::cout;
using  std::endl;
 
class  Stonewt
{
private :
     enum  {Lbs_per_stn = 10};       // pounds per stone
     int  stone;                     // whole stones
     double  pds_left;               // fractional pounds
     double  pounds;                 // entire weight in pounds
     void  Show()  const {
         cout <<  "\t this = "  <<  this  <<  ", (stone, pds_left) = ("  << stone <<  ", "  << pds_left <<  "), pounds = "  << pounds << endl;
     }
public :
     //explicit Stonewt(double lbs);          // constructor for double pounds
     Stonewt( double  lbs);           // constructor for double pounds
     Stonewt( int  stn,  double  lbs);  // constructor for stone, lbs
     Stonewt();                     // default constructor
     Stonewt( const  Stonewt &st);          // copy constructor
     ~Stonewt();
     Stonewt& operator=( const  Stonewt &st);   // copy constructor
     operator  double () const ;
};
 
// construct Stonewt object from double value
Stonewt::Stonewt( double  lbs)
{
     cout <<  "enter "  << __func__ <<  "(double lbs)\n" ;
     stone =  int  (lbs) / Lbs_per_stn;     // integer division
     pds_left =  int  (lbs) % Lbs_per_stn + lbs -  int (lbs);
     pounds = lbs;
     Show();
     cout <<  "leave "  << __func__ <<  "(double lbs)\n" ;
}
 
// construct Stonewt object from stone, double values
Stonewt::Stonewt( int  stn,  double  lbs)
{
     cout <<  "enter "  << __func__ <<  "(int stn, double lbs)\n" ;
     stone = stn;
     pds_left = lbs;
     pounds =  stn * Lbs_per_stn +lbs;
     Show();
     cout <<  "leave "  << __func__ <<  "(int stn, double lbs)\n" ;
}
 
Stonewt::Stonewt()           // default constructor, wt = 0
{
     cout <<  "enter "  << __func__ <<  "()\n" ;
     stone = pounds = pds_left = 0;
     Show();
     cout <<  "leave "  << __func__ <<  "()\n" ;
}
 
Stonewt::Stonewt( const  Stonewt &st)   // copy constructor
{
     cout <<  "enter "  << __func__ <<  "(const &)\n" ;
     stone = st.stone;
     pounds = st.pounds;
     pds_left = st.pds_left;
     Show();
     cout <<  "leave "  << __func__ <<  "(const &)\n" ;
}
 
Stonewt::~Stonewt()          // destructor
{
     cout <<  "enter "  << __func__ <<  "()\n" ;
     Show();
     cout <<  "leave "  << __func__ <<  "()\n" ;
}
 
Stonewt& Stonewt::operator=( const  Stonewt &st)
{
     cout <<  "enter "  << __func__ <<  "(const &)\n" ;
     if ( this  == &st){
         cout <<  "same object\n" ;
         return  * this ;
     }
     stone = st.stone;
     pounds = st.pounds;
     pds_left = st.pds_left;
     Show();
     cout <<  "leave "  << __func__ <<  "(const &)\n" ;
     return  * this ;
}
 
// conversion functions
Stonewt::operator  double () const
{
     cout <<  "enter "  << __func__ <<  "()\n" ;
     Show();
     cout <<  "leave "  << __func__ <<  "()\n" ;
     return  pounds; 
}
 
int  main(){
     Stonewt obj1 = 275;  // uses constructor to initialize
     obj1 = 276.8;       // uses constructor for conversion
     cout <<  double (obj1) << endl;
}

测试结果:

enter Stonewt(double lbs)

         this = 0x7fff20137160, (stone, pds_left) = (27, 5), pounds = 275

leave Stonewt(double lbs)

enter Stonewt(double lbs)

         this = 0x7fff20137180, (stone, pds_left) = (27, 6.8), pounds = 276.8

leave Stonewt(double lbs)

enter operator=(const &)

         this = 0x7fff20137160, (stone, pds_left) = (27, 6.8), pounds = 276.8

leave operator=(const &)

enter ~Stonewt()

         this = 0x7fff20137180, (stone, pds_left) = (27, 6.8), pounds = 276.8

leave ~Stonewt()

enter operator double()

         this = 0x7fff20137160, (stone, pds_left) = (27, 6.8), pounds = 276.8

leave operator double()

276.8

enter ~Stonewt()

         this = 0x7fff20137160, (stone, pds_left) = (27, 6.8), pounds = 276.8

leave ~Stonewt()






      本文转自FrankNie0101 51CTO博客,原文链接:http://blog.51cto.com/frankniefaquan/1934950,如需转载请自行联系原作者






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值