Is using *this a good idea?
c++, this-pointer
Solution
Get a new instructor. It looks as if the declaration of plus() is completely wrong.
- it probably should return a value rather than a reference
- if it must return a reference, it should return a const reference
- it should definitely take a const reference as a parameter
That is for likely sensible implementations of a member plus() function. Of course, it should probably be a friend.
Problem
I'm not sure if ``` return *this ``` is the only way we could return an instance of a class who called a member function? The reason why I asked is because our instructor told us to avoid using pointers if necessary and I'm wondering if this is a case where the only necessary way to do it is by returning the this pointer. I'm working with a fraction class that holds private data members numerator and denominator. The member function I'm talking about is used to add two fractions for example: ``` Fraction C = A.plus(B); ``` plus member function is defined as this: ``` Fraction& plus( const Fraction frac ) ``` The instructor wants us to do C = A += B , so I guess that's why.