References in C++

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


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.

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


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
lampsyAuthor Commented:
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?

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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

lampsyAuthor Commented:
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,


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.

lampsyAuthor Commented:
Thanks very have been most helpful

I will award the points now
I am glad to have helped :)
<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;

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

From novice to tech pro — start learning today.