I think in this case it is safe to use
return *this
because this refers to the current object so it is guaranteed to exist, so it won't be null.
The reason plus returns reference to itself is so that it can be chained:
Fraction C = A.plus(B).plus(D) // perhaps?
Note that in the above case C will be created by copying the result of addition.
This also assumes that operation plus is meant to modify object (in this case A) and return the reference to this modified object.
Wouldn't plus accept reference instead of making copy of the parameter?
Fraction& plus( const Fraction& frac )
This is similar to how you would implement operator= (an example):
A& operator=(const A& right) {
if(this == &right) return *this; // Handle self-assignment
b = right.b;
return *this;
}
Maybe you would want to not modify object and return new object:
// assuming there's a constructor Fraction(int numerator, int denominator):
Fraction* plus(Fraction const& rhs)
{
return new Fraction(numerator * rhs.denominator
+ rhs.numerator * denominator,
denominator * rhs.denominator);
}
But this of course has to return pointer to new instance which is not a reference as maybe required in your task (?).
Or even better:
Fraction plus(Fraction const& rhs)
{
return Fraction(numerator * rhs.denominator
+ rhs.numerator * denominator,
denominator * rhs.denominator);
}
This will create Fraction in the space of calling function so there's no overhead of copying structure on return.