The & symbol means to pass by reference
http://www.tech-recipes.com/rx/1232/c-pointers-pass-by-value-pass-by-reference/
What it allows is the values to be returned back to the caller. In this case you need to return the head pointer to the member array. The array information (which changed) has to be pointed at and we need that pointer. Passing by reference changes where the pointer points and returns the new location. Without it, you would not receive the new array.
For Key::Key(Key &key), really is doing the same as above. Except not as an array, its a singular object of Key type. But if the object is changed, the changes will be seen by the caller of the method aswell. Also, this saves the need of copying from the original to the function variable (i.e. less memory usage and time usage which is important on big arrays). I.e. arrays should best be implemented using the STL library for vectors/lists. So a list example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <list>
void myfunct(std::list<int> myList)
{
// Do something
}
int main()
{
std::list<int> daList;
for (int i = 0; i < 255; i++)
daList.push_back(i);
myfunct(daList);
}
|
What happens above daList is copied into myList via the List copy constructor (which in tern uses the int copy constructor).
http://cplusplus.com/reference/stl/list/list/ If myfunct() changed any values in myList, daList would not see any changes.
Now if I change it to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <list>
void myfunct(std::list<int>& myList)
{
// Do something
}
int main()
{
std::list<int> daList;
for (int i = 0; i < 255; i++)
daList.push_back(i);
myfunct(daList);
}
|
I am for the ease of clarity (but reality is not as simple) passing the pointer of daList to myList and only copying the pointer of daList to myList so they both point to the same location of data and then automatically dereferencing the pointer i.e. a normal list. So any changes to myList will affect daList.
You could overload the = operator(), but if copying type to same type the copy constructor will do the task. If however going class A = class B then you would need to overload the = operator().
1 2 3 4 5
|
char *p;
char *z =3; // you don't want to do this, you just assigned z = memory address 0x03 since z is a pointer
p=&z; //p now points to z // P now points to the address that pointer z is allocated in
or
p=*z; //p now holds value of z //p now points to memory address of what ever value was at address 0x03. i.e. if Mem loc 0x03 has the value 0x0824, then p now points to memory location 0x0824
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
char * p; // p points to unknown location
char * z = new char('3'); //p points to an allocated memory location of size char containing the value of 3
p = z; // p points to the same location as z
std::cout<<*p<<std::endl; //prints 3
std::cout<<*z<<std::endl; //prints 3
*p = '5';
std::cout<<*p<<std::endl; //prints 5
std::cout<<*z<<std::endl; //prints 5
z = new char('8');
std::cout<<*p<<std::endl; //prints 5
std::cout<<*z<<std::endl; //prints 8
|