i have the function: const A& f(…) {…}
a. const A a1 = f(..);
b. const A &a2 = f(...);
which of the is the better one to use? in both cases, if i understand correctly, i prevent the possibility of modifying the returned object.
in the first option, the copy constructor of A will be called – am i correct?
It depends on what you want.
In the first case you create a new const object that is constructed from the returned reference. It will be a snapshot of what was returned and will be valid for its entire lifetime.
In the second you just initialize a reference. This means that any changes to the original object will be visible through the reference but there is a danger that the referred object will be destroyed while the reference is still alive.