2015-04-19 49 views
1

我試圖做一個類矩陣,並試圖將值指數在分配這樣運營商矩陣重載:如何重載()和=同時

M(0,3)=3; 

通過操作符重載。 不能弄清楚如何超載這個運營商 它的原型應該是這樣嗎?

void operator()=(int i,int j,int s);//how to overload? 

float operator()(int i,int j);//this can be used to get that number form that index. 
+0

爲什麼不反過來加載'operator []'? 'operator()'保留用於函數調用。你想用C++操作符構造一種不同的語言嗎? –

回答

2

沒有operator()=。你想讓你的operator()返回對矩陣中元素的引用。然後你可以分配給被引用的元素。

float& operator()(int i,int j); 
const float& operator()(int i, int j) const; 

您可能還需要一個const重載,該重載返回一個const引用。

+0

'const'版本可能只是返回'float'。 –

+0

@ T.C。我只是保持引用來保持它的一致性,因爲在通過值返回和const引用之間仍然存在一些差異。看看[爲什麼我不能在memcpy中使用const參數?](http://stackoverflow.com/q/17669913),其中有人被它弄糊塗了。 –