0

I am writing an Interface for Matrix class in C++
So all functions need to be virtual
Now in the below code I have defined a virtual function virtual Matrix add(Matrix A, Matrix B) const = 0;
The problem I see is the class Matrix is not defined. So I am confused should I define the
class Matrix in the interface ? Or is there a better way to implement the interface

class MatrixInterface
    {
    public:
       
        virtual Matrix add(Matrix A, Matrix B) const = 0;
    };

The updated code that's giving compilation error :
In Matrix.cpp add method : member function declared with 'override' does not override a base class memberC/C++(1455)

#include <iostream>
#include "MatrixInterface.h"
class Matrix : public MatrixInterface
{
public:
    Matrix *add(MatrixInterface *A, MatrixInterface *B) override{
        // implementation
    };
};


class MatrixInterface
{
public:
    virtual MatrixInterface *add(MatrixInterface *A, MatrixInterface *B) const = 0;

};
puneeta
  • 5
  • 3
  • 2
    Why not return a `MatrixInterface` type? C++ allows covariant return types. – Mansoor May 06 '21 at 10:16
  • I guess classes that implement the interface are supposed to act on any class also implementing that interface (including themselves). So why not operate on `MatrixInterface` instead of a `Matrix` class that does not exist? – eike May 06 '21 at 10:19
  • But as `add` is a non static method, you would use it like that: `A.add(B, C)`. Is it really what you want? – Serge Ballesta May 06 '21 at 10:21
  • @SergeBallesta Yeah you are write probably if I am making it non static then I should just have one parameter as argument as the first one should be *this* . Also in this case ideally should I add both `A.add(B)` and `add(A,B)` of these methods in the interface ? – puneeta May 06 '21 at 10:29
  • @eike So you mean since Matrix will implement MatrixInterface I can use MatrixInterface in the argument types. As it will allow the Matrix also to be passed – puneeta May 06 '21 at 10:30
  • 1
    IMHO, it would be simpler for future usage to declare an `operator +` that would return a reference to its `this` object: `MatrixInterface& operator + (const MatrixInterface &B);`. That way you would simply use `A + B`... – Serge Ballesta May 06 '21 at 10:39
  • @puneeta exactly – eike May 06 '21 at 10:44
  • @SergeBallesta that sounds like a `+=`, not a `+` – Caleth Dec 13 '21 at 16:06

1 Answers1

1

Return the interface type from the add method in your interface class, then in the subclass, you can override the virtual method and change the return type to Matrix (or any narrower type).

class MatrixInterface
{
public:
    virtual MatrixInterface* add(MatrixInterface* A, MatrixInterface* B) const = 0;
};

Derived class:

class Matrix : public MatrixInterface
{
public:
    Matrix* add(MatrixInterface* A, MatrixInterface* B) override 
    { 
        // implementation
    };
};

You will need to use pointers to take advantage of covariant return types.

Mansoor
  • 2,357
  • 1
  • 17
  • 27
  • Thanks That makes a lot of sense. But I am getting a compile time error - `Matrix.cpp:6:13: error: ‘Matrix* Matrix::add(MatrixInterface*, MatrixInterface*)’ marked ‘override’, but does not override` and `Matrix *add(MatrixInterface *A, MatrixInterface *B) override{` – puneeta May 06 '21 at 16:28
  • 1
    ok I think I got it. I was not using `const` in the derived class. – puneeta May 06 '21 at 19:08
  • In my example, I've used raw pointers, but you will likely be creating a new matrix, so should consider using a smart ptr instead. – Mansoor May 07 '21 at 08:23