[問題] "+"的運算子多載 單參數VS二參數!
+的運算子多載可以實現物件+物件的可能!
今天小弟測試了+運算子使用單參數和二參數的結果: 結果是一樣的
可否有大大用相加的概念解釋一下呢?(似乎一個是成員函數,一個不是)
還有雙參數一定要配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
04/27 22:09, 1F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章