0

Can any one explain to me the difference between these two functions:

int& test(int a)
{
    return a;
}

and

int test(int a)
{
    return a;
}

and tell me what the purpose when we use '&' operator after return type of a function? When should we use it because I've rarely seen it before.

sashkello
  • 17,306
  • 24
  • 81
  • 109
dinhha471
  • 3
  • 1
  • You generally don't want to use it as the return type for the exact reason displayed here. – chris Oct 03 '13 at 02:28
  • 2
    http://stackoverflow.com/a/2273821/78845 – johnsyweb Oct 03 '13 at 02:29
  • 3
    It is not an "operator". It is just `&` character that happens to be the same character used in some operators. – AnT stands with Russia Oct 03 '13 at 02:30
  • ok, thanks every one, the best thing I got from that link is : "if you need to return a value, return a value and don't worry about any "expense". anyway, thanks for you helps @@ – dinhha471 Oct 03 '13 at 02:46
  • The first method is intended to return a reference of a given variable. It`s normally used when passing parameters. Whereby, you pass a reference as an input. The second method, just return the value of the variable A and not a reference. There is a difference between passing a variable by a value and passing a variable by reference. – Juniar Oct 03 '13 at 02:46

3 Answers3

2

Can any one explain to me the difference between these two functions?

The first example may yield undefined behavior because the returned reference "points" to a variable that, at the end of the function, gets destroyed. This is very similar (almost identical) to the definition of dangling pointer.

The second case is the normal, "correct" way of returning the variable in the example specified above.


and tell me what the purpose when we use '&' operator after return type of a function?

The purpose of a & after a type in the return statement is to return a reference to that type.


When should we use it because I've rarely seen it before.

Generally, passing a reference or returning a reference is useful to avoid making a copy of the parameter being passed/returned, which may be costly.

Since C++11 there's a better way of handling returning big objects: move semantics.

In other cases it is used to provide access to internal members of a class. This is the example of std::string::operator[] for example:

char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;

It may also be used to allow function call chains. This is the case of:

std::cout << 'H' << 'e' << 'l' << 'l' << 'o';

for example. In the code above, the std::ostream::operator<< returns a reference to itself.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I dont know the exactly way to explain what it should be, cause I'm not well at English, but I wonder what the "purpose" when we use & after return type. may be the example below doesn't show the exactly way, that just what I think, sorry about that. I just want to know the 'purpose' when we use it. thanks for you comment :D – dinhha471 Oct 03 '13 at 02:38
  • @dinhha471, Take a look at the new edit. – Shoe Oct 03 '13 at 02:41
  • In my opinion, although returning a reference to an int may be legal 'C', as someone pointed out above, the function is returning a reference to a variable on the functions stack. So, when you try to access it, you may get an access violation or SEGV. – cowboydan Oct 03 '13 at 02:44
  • @Jeffrey : It's just what I wonder when I see some function in C/C++ library. But in real, I never use it so far. Your explaination so good, I couldn't vote for you (cause I'm not enough reputation), Thanks – dinhha471 Oct 03 '13 at 03:14
  • See also http://stackoverflow.com/q/4305673/78845 – johnsyweb Oct 03 '13 at 06:59
0

'&' operator after return type means that the function returns a reference. It makes sense it getter methods like

class A{
public:
     int &val(){return _val;}
private:
     int _val;
};

The advantage is that you can both read and modify the state of the object. For example:

A a;
a.val() = 10;

But it in your case it is probably better to pass a reference to a function rather then return it:

void test(int &a)
{
    //do something with a
}

It is not good to return a reference to a function local object, for example:

int& test()
{
    int x;    
    return x;
}

because x doesn't exists outside test() function.

cpp
  • 3,743
  • 3
  • 24
  • 38
0

The first one is dangerous, because of the variable scope. If you want to do it this way, you should make the variable static.

mars
  • 145
  • 6