Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why sometime a function return a const, or const reference

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?

like image 671
demaxSH Avatar asked May 20 '11 04:05

demaxSH


People also ask

Can a const function return a reference?

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 .

Should I return a const reference?

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.

Can a const function return a non-const reference?

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.

What is the point of a 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.


2 Answers

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.

like image 151
Zan Lynx Avatar answered Oct 01 '22 11:10

Zan Lynx


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.

like image 45
iammilind Avatar answered Oct 01 '22 09:10

iammilind