抽象类(接口)中的运算符重载

时间:2015-03-07 18:20:07

标签: c++ interface operator-overloading abstract-class

我希望有一个抽象类 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;

2 个答案:

答案 0 :(得分:2)

用法看起来应该是这样的: IMatrix<double> M3 = *M1 + *M2; 然而,由于它是抽象类,因此无法创建实例化IMatrix。你唯一能做的就是使operator +返回引用矩阵而不是矩阵值(通过你在运算符结果中输入类型T的方式,这可能不是你想获得的东西)。 / p>

答案 1 :(得分:2)

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

编辑2015年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
}