首页 > 代码库 > 类的转换函数
类的转换函数
转换函数的特点:
(1)必须是类方法。
(2)没有返回值。
(3)没有参数。
(4)只能转换为内置类型,不可以转换为自定义类型。
#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()
本文出自 “用C++写诗” 博客,谢绝转载!
类的转换函数