About returning a reference

Hi, I am going through Jesse Liberty's "Teach Yourself C++ in 21 Days" and found the following program with an example of overloaded assignment operator:

#include <iostream>
using namespace std;

class CAT
{
public:
      CAT();
        ~CAT();
      int GetAge() const {return *catAge;}
      int GetWeight() const {return *catWeight;}
      void SetAge(int age) {*catAge = age;}
      CAT &   operator= (const CAT &);

private:
      int *catAge;
      int *catWeight;
};

CAT::CAT()
{
      catAge = new int;
      catWeight = new int;
      *catAge = 5;
      *catWeight = 8;
}

CAT::~CAT()
{
      delete catAge;
      catAge = 0;
      delete catWeight;
      catWeight = 0;
}

CAT &  CAT::operator=(const CAT & rhs)
{
      if (this == &rhs)
            return *this;
      else
      {
            *catAge = rhs.GetAge();
            *catWeight = rhs.GetWeight();
            return *this;
      }
}

int main()
{
      CAT *Cat1 = new CAT;
      cout << "Cat1's age: " << Cat1->GetAge() << endl;
      cout << "Setting Cat1's age to 6..." << endl;
      Cat1->SetAge(6);
      CAT *Cat2 = new CAT;
      cout << "Cat2's age: " << Cat2->GetAge() << endl;
      cout << "Now setting Cat2 equal to Cat1... \n";
      Cat2 = Cat1;
      cout << "Cat2's age now: " << Cat2->GetAge() << endl;
        return 0;
}


I have a simple question: why does the overloaded assignment operator return a reference to a CAT object rather than a CAT object? Does it make any difference if you change it to return a CAT object instead? I tried changing it and couldn't see any difference in the output; after all all it should do is make a "shallow copy" of Cat1 and point it to the same area in the free store.
Thanks.
RothbardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
It is necessary for writing assignments like:

Cat1 = Cat2 = Cat3;

It is executed by the following way:

Cat1 = (Cat2 = Cat3);

This means, Cat3 value is assigned to Cat2; result of this assignment (Cat2&) is assigned to Cat1.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlexFMCommented:
For example, stream << operators always return stream reference:
ostream& operator<<(int).
If operator << returns void, it is possible to write:
cout << n;
but line
cout << n << endl
is not compiled.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.