[英]operator overloading in abstract class(interface)
我想要一個抽象類IMatrix ,其中包含純虛擬成員,其中一個是運算符重載成員。
template <typename T>
class IMatrix
{
public:
virtual T operator+(const T& b)=0;
};
對於實現,我想使用第三方矩陣類作為封裝實現(techsoft :: matrix innerMatrix)。
#include "IMatrix.h"
#include "cmatrix"
template <typename T>
class ArdalanMatrix :public IMatrix<T>
{
public:
ArdalanMatrix(int r,int c, T val=0.){
numberOfRows = r;
numberOfColumns = c;
innerMatrix.resize(r, c, val);
};
virtual T operator+(const T& b){
return ....??? ;
};
private:
techsoft::matrix<T> innerMatrix;
int numberOfRows;
int numberOfColumns;
};
實際上我不知道如何在ArdalanMatrix類中實現運算符。 最終,我想使用此運算符重載,如下所示:
IMatrix<double> *M1 = new ArdalanMatrix<double>(2, 2, 2);
IMatrix<double> *M2 = new ArdalanMatrix<double>(2, 2, 2);
IMatrix<double> M3 = *M1 + *M2;
用法應類似於: IMatrix<double> M3 = *M1 + *M2;
但是,由於實例化IMatrix是抽象類,因此無法創建實例化。 您唯一可以做的就是使operator +
返回對矩陣的引用,而不是按值返回矩陣(通過在操作符結果中輸入T的方式,這可能不是您想要獲得的東西)。
首先,將您的operator+
定義為以參數為參數並返回T
,這是模板參數,在這種情況下為double
。 這意味着您將像這樣使用它:
double M3 = M1 + 1.5;
但這可能不是您想要的。 您可能希望operator+
也返回一個矩陣,並將另一個矩陣作為參數:
virtual IMatrix<T>* operator+(const IMatrix<T>& b){
return new ArdalanMatrix( /* something */ );
};
然后,您可以像這樣使用它:
IMatrix<double> *M1 = new ArdalanMatrix<double>(2, 2, 2);
IMatrix<double> *M2 = new ArdalanMatrix<double>(2, 2, 2);
IMatrix<double> *M3 = *M1 + *M2;
delete M3; // Must call `delete` because operator+ called `new`!
delete M2; // M2 and M1 must be deleted too...
delete M1;
注意:您需要為IMatrix
聲明一個virtual
析構IMatrix
,否則此處將發生資源泄漏。 這就是為什么 。
但是,這是壞的, delete M3
看起來奇怪,因為你無法看到new
任何地方。 真正的解決方案是使用智能指針:
virtual std::unique_ptr<IMatrix<T>> operator+(const IMatrix<T>& b){
return std::unique_ptr<IMatrix<T>>(new ArdalanMatrix( /* something */ ));
};
然后使用它:
std::unique_ptr<IMatrix<double>> M1(new ArdalanMatrix<double>(2, 2, 2));
std::unique_ptr<IMatrix<double>> M2(new ArdalanMatrix<double>(2, 2, 2));
std::unique_ptr<IMatrix<double>> M3 = *M1 + *M2;
// No need to call `delete` now, unique_ptr does it automatically.
但是,您也可以靜態分配矩陣,這要簡單得多:
ArdalanMatrix<double> M1(2, 2, 2);
ArdalanMatrix<double> M2(2, 2, 2);
ArdalanMatrix<double> M3 = M1 + M2;
// Destruction of M1, M2 and M3 happens automatically.
然后,您可以僅在必要時使用IMatrix
指針/對ArdalanMatrix
對象的引用,例如:
void foo(const IMatrix<double>& m) { ... }
...
foo(M1); // M1 was declared as ArdalanMatrix<double>.
編輯15年4月10日:
最后一種方法的好處是,編譯器負責刪除對象。 而且,靜態分配應該是在C ++中聲明對象的默認方式,而當不夠時,應該使用智能指針。 Raw new
和delete
只能用作不得已的方法。
但是,該解決方案有點有趣,因為由於IMatrix::operator+
是抽象的,因此無法實現返回IMatrix
對象。 稍微解釋一下:
template <typename T>
class IMatrix
{
public:
//virtual IMatrix operator+(const IMatrix& b) = 0;
// impossible, cannot return an abstract object!
virtual IMatrix& operator+=(const IMatrix& b) = 0;
// possible, only returning a reference
};
template <typename T>
class ArdalanMatrix : public IMatrix<T>
{
public:
ArdalanMatrix(int r, int c, T val = 0.0)
: numberOfRows(r), numberOfColumns(c) {
innerMatrix.resize(r, c, val);
};
ArdalanMatrix& operator+=(const IMatrix<T>& b) override {
// (can override with different return type but not parameter)
// modify innerMatrix based on b...
return *this;
};
ArdalanMatrix operator+(const IMatrix<T>& b) {
ArdalanMatrix(*this) copy; // make copy of *this
copy += b; // modify copy based on b using operator+=
return copy; // return it
// or simply: return ArdalanMatrix(*this) += b;
};
private:
techsoft::matrix<T> innerMatrix;
int numberOfRows;
int numberOfColumns;
};
現在您可以執行以下操作:
int main() {
ArdalanMatrix<double> M1(2, 2, 2);
ArdalanMatrix<double> M2(2, 2, 2);
IMatrix<double>& R1 = M1;
IMatrix<double>& R2 = M2;
R1 += R2; // add R2 to R1, works. M1 will get modified
//const IMatrix<double>& R3 = R1 + R2;
// doesn't work, can't add two IMatrices
// However, since we know that R1 and R2 are really ArdalanMatrices,
// we can do this and it works as expected:
const IMatrix<double>& R3 = dynamic_cast<ArdalanMatrix<double>&>(R1)
+ dynamic_cast<ArdalanMatrix<double>&>(R2);
// R3 is now really a ArdalanMatrix too
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.