References in C++

Posted on 2004-11-07
Last Modified: 2010-04-01
Let me disclaim this, as this forms a part of homework that I must complete.

However, I am not looking for a solution to a problem, but an explanation as to what certain lines of code actually do.
I have tried multiple different sources to explain exactly what I am confused about, and can't manage to get anywhere.

So, I come to EE, hoping that this question doesn't get canned (as I understand that it can) because it is technically homework.

Here goes

#include <iostream.h>

class list {
   list () { };
   list (int = 10);

   int getsize (); //returns the size of the list
   const list & operator=(const list &); //assign list --- FIRST LINE THAT CONFUSES ME
   bool operator==(const list &) const; //compare if two lists are equal  --- SECOND LINE THAT CONFUSES ME

   int size; //number of elements in the array
   int *listPtr; //pointer to first element of the array

I understand the following
   - I need to complete the assign and compare functions, in order to test them in a main function.
   - I have to create an array of size 'size'

If someone can explain what the two lines that confuse me actually do/say then I would be most appreciative


Question by:lampsy
    LVL 19

    Accepted Solution

    >> const list & operator=(const list &); //assign list --- FIRST LINE THAT CONFUSES ME
       bool operator==(const list &) const; //compare if two lists are equal  --- SECOND LINE THAT CONFUSES ME

    What exactly is confusing ?

    The first line has set up the assignment operator, so in order to assign one thing to another you have to somehow let the object that is to be assigned to know what you want to assign to it.  This comes in the only parameter.  

    const list&

    We pass a reference to a list to save copying an object which would be expensive.

    You can call a = b where a and b are lists, which is equivalent to


    so we assign b to a.

    The reason we pass a const reference is since there is no need for the assignment operator to alter the object which it is assigning to another.  Why alter b, you are altering a by assigning b to it.  There is no need to change b.

    The second line has the equivalence operator, ==.  This obviously allows us to test if two things are the same, eg

    if (a == b)

    which is the same as

    if (a.operator==(b))

    Again we pass b by reference (the only parameter to the function) to save possibly expensive copying.  A bool is returned obviously to determine the result of the test.

    What you need to do in the first function is essentially carry out the assigning of b to a, i.e. alter all of the members of a to be the same as those in b.  The second (equivalence)  operator is easier; loop through all members of b, testing them against those in a.  As soon as you hit one that is different, return FALSE.


    Author Comment

    Okay, that is making a little more sense.

    Essentially, without the references and consts, the first line is saying that:

    list operator = list;

    What is don't understand about the second line is the extra const at the end of the line.

    Thanks for the info so far...anything more that you think can help me?

    LVL 19

    Expert Comment

    >> Essentially, without the references and consts, the first line is saying that:

    >> list operator = list;

    Without the references, we are stating

    list operator=(list)

    This means the function, operator=, returns a list, and takes a list as a parameter, just like

    int main(void)

    returns an int, and takes no parameters (void).

    When we state

    list& operator=(const list&)

    we are defining a function prototype.  Note how parameter has not got a name, that is how we can define prototypes for functions.  They don't need a name for the formal parameter until we define them.  When we implement this function, we will have to give the parameter a name otherwise we could not ever refer to it.  EG

    list& operator=(const list& rhs)

    here I have given the const list& the name rhs.  Why rhs ?  That is obviously a short hand for right hand side, and, relating back to what I said earlier, about

    a = b

    being the same as


    you can see that b is on the right hand side of the assignment, hence rhs :)

    Now, about the const.

    If I want to assign b to a, I am altering a, not b.  All the const means is that your function makes a promise to the compiler not to alter b in any way.  Why would it want to anyway ?  We are only altering a, by the very nature of assigning to it.  So if I implemented operator = as

    list& operator=(const list& rhs)
       rhs = <something>

    the compiler would complain since I am breaking the promise not to alter it.


    Author Comment

    That cleared up a whole lot of things.

    One last thing...the line -

    bool operator==(const list &) const;

    Has an extra const at the end, which is what I was trying to get at in my last post.
    Is it superfluous, or has it got an actual purpose? If so, what is the purpose?

    Thanks again,


    LVL 19

    Expert Comment

    Ah right.  Sorry.

    This is stating that the function itself is const.  When you call a function on an object, say

    class test
          inline void modify() const {this->i = 10;}
          int i;

    int main()
          test f;

    The const in modify means that the attempt to change the value of i through the 'this' pointer will be invalid.  In essence, the function cannot modify any of the values of the object on which it was called.

    So when you see

    bool operator==(const list &) const;

    that means that the operator== cannot modify the object on which it is called,


    if (a==b)

    which is

    if (a.operator==(b)

    means operator== could not modify a.  Why would it want to ?  It is simply testing equivalence.


    Author Comment

    Thanks very have been most helpful

    I will award the points now
    LVL 19

    Expert Comment

    I am glad to have helped :)
    LVL 30

    Expert Comment

    <iostream.h> is not part of the C++ standard.

    To make your code portable, you should use <iostream>, which is part of the C++ standard.

    #include <iostream>
    using namespace std;


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    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