0

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++

Community
  • 1
  • 1
  • Your first version shows undefined behavior, see [here](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) please. – πάντα ῥεῖ Jul 13 '16 at 09:41
  • 1
    Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) that illustrates your question and the code you're asking about. For example the last example will not compile, and in the first example `localVariable` doesn't seem to actually be a local variable. And what is `value1`? – Some programmer dude Jul 13 '16 at 09:42
  • Try looking at this (Possibly a duplicate) : http://stackoverflow.com/questions/12083451/returning-vs-using-a-reference-parameter – MIftikharK Jul 13 '16 at 09:43
  • 1
    If the first case, I can write `if (getValue() == 5)` without having to first create a separate variable to store the value. Huge advantage. – Bo Persson Jul 13 '16 at 09:45
  • 1
    @BoPersson Unfortunaltely this 1st version doesn't work. – πάντα ῥεῖ Jul 13 '16 at 09:54
  • variable name is misleading as `localVariable` is not a local variable... – Jarod42 Jul 13 '16 at 09:58
  • after your edit I dont understand the question anymore. It is pointless to write a function that merely returns the value of a globally defined variable. In this case the best would be not to write a function at all but access the variable directly. – 463035818_is_not_an_ai Jul 13 '16 at 11:10
  • The variable is not global. it is meant to be a member variable. My class is like an interface class so other classes should get access to my variables values via get-functions. – Pandabaerchi Jul 13 '16 at 14:46
  • @πάντα ῥεῖ Could you please explain why this doesn't work? When the Instance of this Class is alive it would work or not? – Pandabaerchi Jul 14 '16 at 09:27
  • @Pandabaerchi You example 1st returned a reference to a local variable, why this doesn't work is described in the post in linked. IIf that is class member code, make that clear in your example. – πάντα ῥεῖ Jul 14 '16 at 09:35

2 Answers2

0

Return a const/reference or an object by value has the advantage that it can be used in an expression, while using a reference needs a variable where to copy the object.

Usually, pass/return by value is used for small objects and const/reference if they are larger. Note that pass/return by reference also has a cost. Weel, this has changed in c++11 because of the move copy/assignment.

On the other hand --you have used the name "localVariable"-- if you return a reference, you have to take into account that the returned variable must still exist after call return.

EFenix
  • 831
  • 4
  • 11
-4

(The second one isn't a function, its a method).

Reference or not, its a case of readability, the question as asked is more function vs method. A function should return its primary objective, not set an output parameter. Typically a function typically operates on the parameters passed in and is algorithmic in nature.

(actually the example above looks like an encapsulated property)

Vman
  • 3,016
  • 2
  • 24
  • 20
  • What qualifies a _"method"_? It's a function as any other is. – πάντα ῥεῖ Jul 13 '16 at 09:52
  • No its not, a function by definition has to return a value, a method does not have to. Setting an output parameter does not (clearly) signify the programmers intent that it the primary return value. – Vman Jul 13 '16 at 09:56
  • "a function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.". A void return is not a function its a method (or procedure) – Vman Jul 13 '16 at 10:02