class& vs &class

I have fully read this tutorial: http://www.cplusplus.com/doc/tutorial/, which is great by the way.
According to http://www.cplusplus.com/doc/tutorial/pointers/, I understand
 
  int *myPointer;

is a pointer.
But in http://www.cplusplus.com/doc/tutorial/templates/ I see the symbol & after a class name:
 
CVector CVector::operator+ (const CVector& param) {

I can't understand why it's writen CVector& and not CVector since the param is considered an object, not a pointer, since I read param.x and not param->x.

I hope I made well my question.
Thank you.
To clarify consider these two functions :

//This:
void swap(int *a, int *b)
{
int temp = *a;
*a=*b;
*b=temp;

}

//And this:
void swap(int &a, int &b)
{
int temp = a;
a=b;
b=temp;
}
Both of the functions above take reference of 2 variables (representation is different) but if the arguments passed in either if these functions are changed in the function then they will retain their value even after the function is over.


About you question why is there a CVector& param instead of CVector param, It is simple because if you don't take reference of the object of type CVector then the Copy constructor will be called again and again (recursively) as:

CVector func(CVector param);

will take argument like:
//Calling function
func(obj);


this "func(obj);" call is equal to (CVector param = obj;) and this will call the copy constructor recursively.

For better understanding please refer to:

http://stackoverflow.com/questions/8436467/why-is-copy-constructor-not-allowed-pass-by-value

It says: "Passing by value means that the parameter is copied into the function. That calls the copy constructor.

If your copy constructor parameter is pass-by-value... It would call itself... over and over again..."
Thank you for the answer.
However, I am still not sure if I have understood one part of the answer.
In this case:
1
2
3
4
5
6
CVector operator+ (const CVector& lhs, const CVector& rhs) {
  CVector temp;
  temp.x = lhs.x + rhs.x;
  temp.y = lhs.y + rhs.y;
  return temp;
}

both lhs and rhs are not modified, so why can't I define them as CVector, instead of CVector?, ie, why can't I pass them as value instead of reference? In this example I don't see any copy constructor.

Thank you again.
Because passing by value makes copy of argument and destroys it at the end. Which can be quite costly.
When passing by reference you also do not have to worry if object is copyable at all.
Tatanan you can not see the copy constructor but it will be called implicitly when ever you pass any argument in overloaded operator if you define them as CVector. (see the link once which I gave you above.)

While If you use (const CVector& lhs ....) then this will also not change anything but because it is a reference that has been passed as an argument it will avoid calling the copy constructor.

Note: Each time you pass something as an argument in a function that take user-defined types; The copy constructor is called weather you explicitly define it or not.
Understood.
Thank you very much.
Topic archived. No new replies allowed.