我想要一个抽象类 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;

最佳答案

首先,您的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;

注意:您需要为virtual声明一个IMatrix析构函数,否则此处将发生资源泄漏。 Here's why

但这很不好,因为您在任何地方都看不到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++中声明对象的默认方式,而当不够时,应该使用智能指针。原始newdelete仅应作为最后的手段。

但是,该解决方案有点有趣,因为由于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
}

10-06 07:18