[問題] 有關C++Primer中文版,p461頁(12.4.4節)

看板C_and_CPP作者 (....)時間12年前 (2013/07/23 21:10), 編輯推噓2(204)
留言6則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) none 問題(Question): C++ Primer (4ed) p461 12.4.4 真的很奇怪;看了幾次不能推敲出它的意思 所以想上網問一下專家 以下是我盡量湊成的程式碼(我想先嘗試沒有explicit關鍵字的情形) #include <string> #include <iostream> using namespace std; class Sales_item { private: string data; //放isbn字串 public: Sales_item(const string& book); Sales_item(istream& is); bool same_isbn(const Sales_item& that) const; }; Sales_item::Sales_item(const std::string &book) : data(book) { } Sales_item::Sales_item(istream& is) { is>> (*this); //為什麼要這樣寫呢?? 如果是is>>data;我就可以了解了... } bool Sales_item::same_isbn(const Sales_item& that) const { return( data == that.data); } int main() { Sales_item item; cout<< item.same_isbn("999")<<endl; //上面這一行我一直都不能編譯成功... //除非改成 item.same_isbn(string("999")) cout<< item.same_isbn(cin)<<endl; return 0; } 有網友可以給一點提示嗎?? -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.39.90.40

07/23 21:24, , 1F
#inculde <string> ??
07/23 21:24, 1F
※ 編輯: easterday 來自: 114.39.90.40 (07/23 21:30) ※ 編輯: easterday 來自: 114.38.232.217 (07/24 10:24)

07/25 02:35, , 2F
可能是你的建構子沒有支援 char* 型態的版本
07/25 02:35, 2F

07/25 02:57, , 3F
is >> (*this); // 這個敘述是可以由自已決定 istream 物件
07/25 02:57, 3F

07/25 03:00, , 4F
讀取資料後到你指定類別內的資料成員
07/25 03:00, 4F

07/27 19:28, , 5F
因為前面那邊沒有講到 你的問題在14.2.2會有解答 他其實
07/27 19:28, 5F

07/27 19:28, , 6F
是呼叫他額外寫的operator>>來幫他處理這件事
07/27 19:28, 6F
文章代碼(AID): #1Hxe3N4b (C_and_CPP)