So I understand the idea of the "&". But I have some applications were I wanted to make sure I understand the effect in specific situations. First, I will give a simple class and then pose the questions...
So say that I have a class called "personClass". Basically this...
1 2 3 4 5 6 7 8
|
class personClass
{
private:
int height;
string name; //include <string>
//functions coming later
}
|
Question 1: In a function declaration like...
|
personClass& setFirstName(int a, string b);
|
If I just called it like the following example, because of the ampersand that would modify the class used to call it right?
Example:
1 2
|
personClass classA;
classA.setFirstName(5,Bob);
|
So whatever the function did, changes would be on classA?
Question 2: This question is about overloading, say for example, the equals sign. Here's the declaration in the class:
|
const personClass& operator=(const personClass&);
|
That is an example from my book with no explanation. So why do you need the ampersands in both places and how does the const effect this statement? In my mind I get the ampersand, so then I am modifying the actual class right? But then doesn't the const make it so it does't? The ampersand in both places is confusing me because shouldn't it only be needed the first time and then not be const?