Re: What to return -- object, reference or const reference
On Mar 6, 4:10 am, Arv <arvind.b...@gmail.com> wrote:
After writing code in Java for sometime, one thing that I am not
comfortable at all with C++ is the return type of an object.
In java, i do not remember ever seeing a method return a final
reference -- though a const is not equivalent to final, it is
confusing when i see some methods returning a const reference.
I initially assumed returning an object is same as returning a
reference, because when i call the method as
Myclass O1;
O1 = func();
Does it matter if func() returns a reference, const reference or a
Object, anyways the assignment operator is going to get called.
But I am a bit confused when I call AnotherFunc(func())... so is it
possible that if I dont pass a const reference, and the AnotherFunc
accepts a reference, it can potentially change the object I pass.!
Especially this is more confusing in a getter method, should a getter
return a const reference always, because I do not want ppl to modify
the reference they get by calling a getter.
Sorry if this is a bit basic, but I have not been able to understand
this very clearly...
The below rules cover most situations for both parameters and return
values.
Returning by const reference is an optimization over returning by
value that you can use when you know that the object returned will
outlive the function returning it, the caller will not likely be
making a copy of its own, but be sure to let the caller know (through
documentation) how long that reference will be good for.
Returning by non-const reference is only for special situations where
the object being returned is either something that the caller has
access to anyway (for example returning a reference parameter that was
passed in, or a global,) or something that the function is
specifically designed to allow access to (for example containers, and
lazy initializers.)
Function parameters should be passed in by non-const reference only
when the function is specifically designed to modify the object passed
in (for example if the parameter is being used as output, or input and
output.) Otherwise the parameter should be by value if its size is
known to be equal to or less than sizeof(int) and the object
constructor (if it has one) is known not to allocate memory.
Otherwise, the parameter should be a const reference.
You may notice that I didn't mention pointers in the above, they
generally follow the same rules as references when you are talking
about the object the pointer points to, they follow the same rules as
objects when you are talking about the pointer object *itself*.
In summary:
T func(); // default
const T& func(); // only when you know that the value returned will
outlive func
// and its use by the caller
T& func(); // only if the caller has access to the object anyway, or
you specifically
// want to grant access to it
void func( const T& t ); // default
void func( T t ); // if the memory required by the object is known to
be small
void func( T& t ); // if 't' is being used as an output parameter