The course I am taking says that returning this object reference (using *this) is mainly for chaining equal signs (For example: b = d = c = a) However, I cannot understand how this return works while I am overloading the '=' operator. Is it also necessary if I am not going to use any kind of chain? How does this return statement works? Thank you very much.
class Rational {
int _n = 0; // '_n' stands for numerator
int _d = 1; // '_d' stands for denominator
public:
Rational (int numerator = 0, int denominator = 1) : _n(numerator), _d(denominator) {};
Rational (const Rational & rhs) : _n(rhs._n), _d(rhs._dd) {};
~Rational ();
int numerator() const { retrun _n; };
int denominator() const { return _d; };
Rational & operator = (const Rational &);
Rational operator + (const Rational &) const;
Rational operator - (const Rational &) const;
Rational operator * (const Rational &) const;
Rational operator / (const Rational &) const;
};
Rational & Rational::operator = (const Rational & rhs) {
if(this != &rhs){
_n = rhs.numerator();
_d = rhs.denominator();
}
return *this;
}
Rational Rational::operator + (const Rational & rhs) const {
return Rational((_n * rhs._d) + (_d * rhs._n), (_d * rhs._d));
}
Rational Rational::operator - (const Rational & rhs) const {
return Rational((_n * rhs._d) + (_d * rhs._n), (_d * rhs._d));
}
Rational Rational::operator * (const Rational & rhs) const {
return Rational((_n * rhs._n), (_d * rhs._d));
}
Rational Rational::operator / (const Rational & rhs) const {
return Rational((_n * rhs._d), (_d * rhs._n));
}
Rational::~Rational(){
print("dtor: %d/%d\n", this->_n, this->_d);
_n = 0; _d = 1;
}
std::ostream & operator << (std::ostream & o, const Rational & r){
return o << r.numerator() << "/" << r.denominator();
}
int main(int argc, char** argv){
Rational a = 7; // 7/1
cout << "a is: " << a << endl;
Rational b(5, 3); // 5/3
cout << "b is: " << b << endl;
Rational c = b; // Copy constructor
cout << "c is: " << c << endl;
Rational d; // Default constructor
cout << "d is: " << d << endl;
d = c; // Assignment constructor
cout << "d is: " << d << endl;
Rational & e = d; // Reference
d = e; // Assignment to self!
cout << "e is: " << e << endl;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
return 0;
}