First of all, your operator+
is defined to take as parameter and return T
, which is the template parameter, i.e. double
in this case. It means you would be using it like this:
double M3 = M1 + 1.5;
But that's probably not what you want. You probably want the operator+
to return a matrix as well, and take another matrix as a parameter:
virtual IMatrix<T>* operator+(const IMatrix<T>& b){
return new ArdalanMatrix( /* something */ );
};
Then you can use it like this:
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;
Note: You need to declare a virtual
destructor for IMatrix
, otherwise you'll have a resource leak here. Here's why.
But this is bad, the delete M3
looks weird because you can't see new
anywhere. A real solution would be to use smart pointers:
virtual std::unique_ptr<IMatrix<T>> operator+(const IMatrix<T>& b){
return std::unique_ptr<IMatrix<T>>(new ArdalanMatrix( /* something */ ));
};
Then use it:
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.
But you could also allocate the matrixes statically, which would be a whole lot simpler:
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.
Then you can just use IMatrix
pointers / references to the ArdalanMatrix
objects when necessary, e.g.:
void foo(const IMatrix<double>& m) { ... }
...
foo(M1); // M1 was declared as ArdalanMatrix<double>.
Edit Apr 10 '15:
The benefit of the last approach is that the compiler takes care of deleting the objects. Moreover, static allocation should be default way to declare objects in C++, and when that's not enough, smart pointers. Raw new
and delete
should only be used as a last resort.
However, the solution is a bit funny because IMatrix::operator+
can't be implemented returning an IMatrix
object since it's abstract. To explain it a little:
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;
};
Now you can do this:
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
}