First, why return const? say I have
friend const MyVec operator-(const MyVec& left, const MyVec& right)
so is returning const makes me cannot do:
mva - mvb = mvc;
Second, why return const reference? if there is:
friend const MyVec& operator++(MyVec& v)
with const I cannot: (++mva) = mvc
if it is
MyVec& operator++(MyVec& v)
I can:++(++mva)
// with increment twice.
am I understanding right?
You must either change the method to being non-const, return a const reference, or make Bar exempt of the promise by marking it as mutable . E.g.: mutable int Bar; The mutable keyword tells the compiler that the logical constness of the object does not depend on the state of Bar .
You want to return a const reference when you return a property of an object, that you want not to be modified out-side of it. For example: when your object has a name, you can make following method const std::string& get_name(){ return name; }; . Which is most optimal way.
If the thing you are returning by reference is logically part of your this object, independent of whether it is physically embedded within your this object, then a const method needs to return by const reference or by value, but not by non-const reference.
- const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it's a reference to const.
There is not any good reason to return a const object. However, there are many good reasons to return pointers or references to const objects.
The program might have an object that is very expensive to copy, so it returns a reference. However, the object should not be changed through that reference. For example, it might be part of a sorted data structure and if its values were modified it would no longer be sorted correctly. So the const keeps it from being modified accidentally.
Arithmetic operator functions should not return const objects, because of exactly the problems in your question.
Dereferencing an iterator should return a const reference. That is if it is operating on a collection of const objects or possibly on a const collection. This is why class functions sometimes have two copies of a function with the second copy using a const on the function itself, like this:
T& operator[](size_t index);
const T& operator[](size_t index) const;
The first function would be used on non-const objects and the second function would be used for const objects.
Yes your understanding is correct. To avoid accidental assignments, one can return an object by const
or const reference
.
With operator -
, you return an object by value. To avoid it getting edited accidently, one can return by const
value, because anyways the object will be mostly a temporary.
For operator ++
, conventionally it returns reference, however to avoid situations as (++ x) = y;
you can return it by const
reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With