lampsy
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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
>> 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
ASKER
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
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
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
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
ASKER
Thanks very much...you have been most helpful
I will award the points now
I will award the points now
I am glad to have helped :)
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;
<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;
ASKER
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