作者mosluce ()
看板C_and_CPP
標題[問題] cin >> 自訂類別物件
時間Wed Sep 30 09:33:46 2009
舉例來說
class A{
public:
A();
~A();
int value(){
return _value;
}
prvate:
int _value;
}
int main(){
A a();
cin >> a; //這邊要如何設計
cout << a.value(); //這邊才可以輸出所輸入的值呢?
cout << a; //當然也想要可以這麼玩
return 0;
}
有解嗎^^?
朝著運算子重載去思考過,不過弄不出來呢(汗
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.85.58.90
※ 編輯: mosluce 來自: 219.85.58.90 (09/30 09:34)
1F:推 layan:istream& operator>>(istream& in, A& a); 09/30 09:44
2F:→ layan:ostream& operator <<(ostream& out, const A& a); 09/30 09:45
3F:推 VictorTom:其實用 >> 或 << 搜尋一下板面就有討論了....XD 09/30 09:45
4F:→ layan:just implement these two global functions 09/30 09:45
5F:→ mosluce:解決!!!感謝各位大大的指導唷^^ 09/30 10:03