[問題] cin >> 自訂類別物件

看板C_and_CPP (C/C++)作者時間16年前 (2009/09/30 09:33), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串1/2 (看更多)
舉例來說 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)

09/30 09:44, , 1F
istream& operator>>(istream& in, A& a);
09/30 09:44, 1F

09/30 09:45, , 2F
ostream& operator <<(ostream& out, const A& a);
09/30 09:45, 2F

09/30 09:45, , 3F
其實用 >> 或 << 搜尋一下板面就有討論了....XD
09/30 09:45, 3F

09/30 09:45, , 4F
just implement these two global functions
09/30 09:45, 4F

09/30 10:03, , 5F
解決!!!感謝各位大大的指導唷^^
09/30 10:03, 5F
文章代碼(AID): #1AmhLxCs (C_and_CPP)
文章代碼(AID): #1AmhLxCs (C_and_CPP)