c++ operator typename 轉換函式

心鑫發表於2014-03-22
class Stock10 {
private:
	int t = 1;
public:
	friend ostream& operator<<(ostream&, const Stock10&);
	Stock10(int _t);
	operator double();
};
Stock10::operator double() {
	return t;
}

Stock10 s1 = 10; //建構函式加explicit,則禁用這種轉換
	double t2 = s1;

operator double();

將類轉換成相應的資料型別

與輸值=操作符相反 

相關文章