0

Hello I have got 2 questions.

1) I have got math vector which is saved as table. Got dim. I want to overload + and += operators. I have no problem with overloading the += operator, but what should I do if I want to overload the + operator? Should I return a new object of MathVector?

Code for += overload:

MathVector & operator+=(MathVector & o){
        for(int i=0; i<dim; i++){
            this->tab[i]+=o.tab[i];
        }
        return *this;
}

2) If I want to save the result from:

MathVector v1(3);
MathVector v2(3);
v1.fill(); // fill from basic input
v2.fill();
MathVector result;
result=v1+v2;

Should I make special overload for = operator? And another one if I want make something like v1+a (a is integer) ?

masterq007
  • 37
  • 4
  • 1. change `MathVector & o` to `const MathVector & o` and I'd say you've got this done. – user4581301 May 26 '16 at 20:39
  • 1
    `+` should return a new object because it is "functional" - you don't want it to _modify_ its left or right operands, so you have no choice, really. – davidbak May 26 '16 at 20:42
  • 2. depends on `MathVector`. Wholeheartedly recommend reading [What is the Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) for a good rundown on when and why you want to make your own `operator=` – user4581301 May 26 '16 at 21:03

0 Answers0