转换函数的特点:
(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()