0

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!

  • 6
    Look at operator overloading in C++. – Bill Dec 09 '13 at 19:31
  • You're looking for http://stackoverflow.com/questions/4421706/operator-overloading. More specifically, for this: http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719. – caps Dec 09 '13 at 19:44

1 Answers1

1

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.

Community
  • 1
  • 1
rami1988
  • 156
  • 4
  • But how can you do the same thing with third party class such as set? Having a function set Subtract(set A, set B) does not count. – h9uest Feb 06 '15 at 14:41