[問題] "+"的運算子多載 單參數VS二參數!

看板C_and_CPP (C/C++)作者 (no元)時間16年前 (2009/04/27 20:08), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
+的運算子多載可以實現物件+物件的可能! 今天小弟測試了+運算子使用單參數和二參數的結果: 結果是一樣的 可否有大大用相加的概念解釋一下呢?(似乎一個是成員函數,一個不是) 還有雙參數一定要配friend? 很抱歉雖然收尋了許多文章,但還是沒什麼頭緒.... (小弟的程設老師很威!開學才不過九個禮拜C++從0開始到現在已經上到動態陣列了!) class testAdd { private: int a,b; public: int get_a() const; int get_b() const; testAdd(); testAdd(int c,int d); friend const testAdd operator +(const testAdd& testClass,const testAdd& testClass2) { return testAdd(testClass2.get_a()+testClass.get_a(), testClass2.get_b()+testClass.get_b()); } /* testAdd operator +(const testAdd& testClass) { return testAdd(this->get_a()+testClass.get_a(), this->get_b()+testClass.get_b()); } */ void show(); }; .................副程式函式定義..... int main() { testAdd test1(4,6),test2,test; test =test1+test2; test.show(); system("pause"); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.181.63

04/27 22:09, , 1F
9周差不多吧.後面半學期應該都是指標結構了
04/27 22:09, 1F
文章代碼(AID): #19zQ0n7f (C_and_CPP)
文章代碼(AID): #19zQ0n7f (C_and_CPP)