What are the advantages and disadvantages of returning a reference in comparison to passing a reference as parameter.
I have a class which has a member variable ("localVariable" in the little code example) and via get-functions other classes should get the values of these member variables.
At the moment I have the following function:
in int localVariable;
int getValue(){
return localVariable;
}
I was said that it's important so use const references
Which of the following functions would be appropriate? Which are the advantages and disadvantages of every version?
int localVariable;
int const & getValue(){
return localVariable;
}
or
int localVariable;
void const getValue(&refValue){
refValue = localVariable;
}
I found something a bit similar but it's not exactly the same and so it is still not clear to me what is better in my case: Returning a pointer vs. passing a reference to an object to store the answer in C++