2014-02-26 31 views
1

說我有一個名爲Matrix的類,其中括號運算符已重載。我還有一個子類:childMatrix(從Matrix派生),括號操作符以不同的方式重載。使用父類的運算符而不是子類

我有一個對象childMatrix,但我想使用矩陣(父類)的運算符進行一些計算,這可能嗎?

我想:

childMatrix& m; 
m(1,1) = 1; // works fine 
(Matrix)m(1,1) = 1 //error 
(Matrix&)m(1,1) = 1 // error (no match for ‘operator=’ (operand types are ‘Matrix’ and ‘int’)) 

回答

1

是的,這是可能的,你是在正確的軌道上;你只是混了運算符優先級:

((Matrix&)m)(1,1) = 1; 

Live example

這是假設operator()virtual;如果是這樣,你不得不採取相當醜陋的語法:

m.Matrix::operator()(1,1) = 1; 
+0

謝謝!這解決了我的問題! – Winten

相關問題