C++ '-' operator on 'Matrix' class

So I have a Matrix class that returns an array of doubles, e.g:

Matrix A = {0,1,2}
           {3,4,5}

Matrix B = {5,6,7}
           {8,9,10}

I want to perform the operation:

Matrix C = A - B;

I know that the logic would be to call a member function that notices the '-' operator, and have it subtract each element from each other

e.g

   for(i = 0; i < 5; i++){
       C[i] = A[i] - B[i]; 
    }

Am I correct in thinking this and how would I implement this? How do I invoke the operator?

Thanks in advance!

Upvotes: 0

Views: 98

Answers (1)

rami1988
rami1988

Reputation: 156

Yes, you are correct.

To do the operation like this:

Matrix C = A - B;

You will need to overload the '-' operator for your Matrix class and define the subtraction behavior there. Refer to Operator overloading for an introduction.

Upvotes: 1

Related Questions