類的轉換函式

技術小胖子發表於2017-11-15

轉換函式的特點:

(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 << "  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)
"
;
    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)
"
;
}
 
// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs)
{
    cout << "enter " << __func__ << "(int stn, double lbs)
"
;
    stone = stn;
    pds_left = lbs;
    pounds =  stn * Lbs_per_stn +lbs;
    Show();
    cout << "leave " << __func__ << "(int stn, double lbs)
"
;
}
 
Stonewt::Stonewt()          // default constructor, wt = 0
{
    cout << "enter " << __func__ << "()
"
;
    stone = pounds = pds_left = 0;
    Show();
    cout << "leave " << __func__ << "()
"
;
}
 
Stonewt::Stonewt(const Stonewt &st)  // copy constructor
{
    cout << "enter " << __func__ << "(const &)
"
;
    stone = st.stone;
    pounds = st.pounds;
    pds_left = st.pds_left;
    Show();
    cout << "leave " << __func__ << "(const &)
"
;
}
 
Stonewt::~Stonewt()         // destructor
{
    cout << "enter " << __func__ << "()
"
;
    Show();
    cout << "leave " << __func__ << "()
"
;
}
 
Stonewt& Stonewt::operator=(const Stonewt &st)
{
    cout << "enter " << __func__ << "(const &)
"
;
    if(this == &st){
        cout << "same object
"
;
        return *this;
    }
    stone = st.stone;
    pounds = st.pounds;
    pds_left = st.pds_left;
    Show();
    cout << "leave " << __func__ << "(const &)
"
;
    return *this;
}
 
// conversion functions
Stonewt::operator double()const
{
    cout << "enter " << __func__ << "()
"
;
    Show();
    cout << "leave " << __func__ << "()
"
;
    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,如需轉載請自行聯絡原作者


相關文章