reference operator

i'm pretty much a beginner c++ programmer and came across this the other day when learning overload operators:

and in this code:

1
2
3
4
5
6
  CVector& CVector::operator= (const CVector& param)
{
x=param.x;
y=param.y;
return *this;
}


i don't really understand the reference (&) in the first line (CVector& CVector), i learned from this sites tutorial and i know it shows a reference to a variables address in the memory but cant seem to understand its purpose here is there any info on these occasions as i cant even understand how to simulate a question about it to put in google.
thanks for any help in advance.
There are two references in that line.

One reference is the param. When passing possibly large arguments, you want to pass them by reference. This avoids unnecessarily copying them if passed by value.

The other reference is the return type (CVector&). Note that CVector:: is part of the function name and not part of the return type. The CVector & return type indicates that a reference to a CVector is being returned. Note the *this on line 5. By dereferencing the this pointer, we create a reference to the current object. Returning a reference allows operators to be chained. e.g.
1
2
3
  CVector cv1, cv2, cv3;
  //  Initialize cv1 somehow
  cv3 = cv2 = cv1;  // Chained assignment 

Last edited on
Note: Chaining assignments does not require references. It does not matter whether the value of cv3 is copied from reference to cv2 or from unnamed temporary copy.

Similarly, chaining additions does not require references:
cv4 = cv3 + cv2 + cv1;

Chained stream input and output does depend on non-const references though:
std::cout << "Hello" << " world";
is same as
(std::cout << "Hello") << " world";
is effectively same as
1
2
std::cout << "Hello";
std::cout << " world";



A reference is an alias; an alternative, additional name for a variable.
1
2
3
4
int foo = 7;
int & bar = foo;
bar = 42;
// value of foo is now 42 



The caller of CVector::operator= () can decide whether they create a reference to the returned object or make a copy. If the function would return a non-reference, then the caller would have only one option: to make a copy.

A example of common use of returned reference:
1
2
std::vector<int> gaz( 5 );
gaz[0] = 7;

The vector's []-operator returns a reference ...


Perhaps the CVector has other member functions too?
Let say it has a CVector::normalize() that changes the object.
Which looks more convenient?
1
2
3
4
5
cv2 = cv1;
cv2.normalize(); // cv2 is a normalized copy of the cv1

// or
(cv2 = cv1).normalize(); // cv2 is a normalized copy of the cv1 

Probably the first one, but what if you should have all that within a conditional expression?
thanks for the information! it really helped me understand the topic! i have only one more question where would you recommend to find practice exercises on classes ?
NOTE: i learned all of the topics from the sites tutorial until Friendship and inheritance (not included).
Topic archived. No new replies allowed.