About returning a reference

Posted on 2006-03-26
Last Modified: 2010-04-01
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
      int GetAge() const {return *catAge;}
      int GetWeight() const {return *catWeight;}
      void SetAge(int age) {*catAge = age;}
      CAT &   operator= (const CAT &);

      int *catAge;
      int *catWeight;

      catAge = new int;
      catWeight = new int;
      *catAge = 5;
      *catWeight = 8;

      delete catAge;
      catAge = 0;
      delete catWeight;
      catWeight = 0;

CAT &  CAT::operator=(const CAT & rhs)
      if (this == &rhs)
            return *this;
            *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;
      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.
Question by:Rothbard
    LVL 48

    Accepted Solution

    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.
    LVL 48

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now