C++ Return value, reference, const reference
There is no difference unless you write something weird like
(v1 += v2) = v3;
In the first case, the assignment will be to a temporary, and the overall effect will be v1 += v2
.
In the second case, the assignment will be to v1
, so the overall effect will be v1 = v3
.
In the third case, the assignment won't be allowed. This is probably the best option, since such weirdness is almost certainly a mistake.
Why returning of reference is better than returning of value?
It's potentially more efficient: you don't have to make a copy of the object.
and why returning of const reference is better than returning of not-const reference?
You prevent weirdness like the above example, while still allowing less weird chaining such as
v1 = (v2 += v3);
But, as noted in the comments, it means that your type doesn't support the same forms of (ab)use as the built-in types, which some people consider desirable.
Value:
Returning by value means that you are returning a copy of an object. This puts requirements on the class (it has to be copyable or moveable). This means that for object of some classes returning by value may be expensive (in a case where RVO or NRVO does not work or is switched off). This also means that the new object is independent (subject to its design) from other objects and is a value of its own. This is what you probably should return from many binary operators like +, -, * and so on.
Non-const reference:
You really return an alias for another object. The alias being non const allow you to modify aliased object. This is what you should return from some unary oprators like prefix ++ and --, and * (dereference) as you usually want to have the ability to modify returned object.
This is returned by operator>> and operator<< overloaded for streams. This allows chaining of operators:
cout << 5 << "is greater then" << 1 << endl;
cin >> myInt >> myFloat;
You can also return reference to *this when you want to allow chaining of regular methods like this:
object.run().printLastRunStatistics();
Const reference:
Like above but you CANNOT modify aliased object. May be used instead of returning by value when the object to be returned is expensive to copy and when you can ensure its existence after you return from a function.
This is what operator= usually returns to allow multiple assignments in a way standard types support them:
a = b = c;
Const-reference used in operator= prevents this kind of usage (not supported by standard type as far as I remember):
++(a = b);
which would be allowed if normal reference was used.
The difference between return-by-value and return-by-reference takes effect during run-time:
When you return an object by-value, the copy-constructor is called, and a temporary instance is created on the stack.
When you return an object by-reference, all the above does not take place, leading to improved performance.
The difference between return-by-reference and return-by-constant-reference has no run-time effect, and is simply there to protect you from writing erroneous code.
For example, with Vector2D& operator += (const Vector2D& vector)
, you can do:
(x+=y)++
or (x+=y).func()
where func
is a non-const function in class Vector2D
.
But with const Vector2D& operator += (const Vector2D& vector)
, the compiler will generate an error for any such similar attempt.