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 {
   public:
   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

   private:
   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

Thanks,

Mark
lampsyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mrwad99Connect With a Mentor Commented:
>> 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

a.operator=(b)

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.

HTH
0
 
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?

Mark
0
 
mrwad99Commented:
>> 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

a.operator=(b)

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.

HTH
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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,

Mark

0
 
mrwad99Commented:
Ah right.  Sorry.

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

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

int main()
{
      test f;
      f.modify();
}

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,

eg

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.

HTH
0
 
lampsyAuthor Commented:
Thanks very much...you have been most helpful

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

0
All Courses

From novice to tech pro — start learning today.