Returning references in c++

1
2
3
4
5
6
7
8
9
10
int& returnC() 
{
    return c;
}

int& returnC2() 
{
    int *d = &c;
    return *d;
}


Assume that c and d are simple integers.
Why does the code written above work perfectly? Even though c isn't a reference variable, the function returnC() returns the integer without problems. Why does this happen? Thanks for any answers!
ChosenTorture wrote:
Assume that c and d are simple integers.

That's hard to assume given that you've written int *d.

Why are you expecting them to fail? Both functions are returning an integer as expected and the return type specifier to return by address.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <typeinfo>

int c = 42;  // I'm evil

int& getC()
{
  std::cout << "c is a " << typeid( c ).name() << std::endl;
  return c;
}

int& getC2()
{
  int *d = &c;
  std::cout << "d is a " << typeid( d ).name() << " (dereferenced is " << typeid( *d ).name() << ")\n";
  return *d;
}

int main( int argc, char* argv[] )
{

  std::cout << "Address of c is " << &c << std::endl;

  int &a = getC();
  int &b = getC2();

  std::cout << "a: " << a << "\tadd: " << &a << std::endl;
  std::cout << "b: " << b << "\tadd: " << &b << std::endl;

  return 0;
}


Edit: You should generally be very careful when returning references. In a lot of situations it won't end well.
Last edited on
I'm not expecting them to fail. I don't understand the reason why they work.

A function with a type of int& should return reference variables. But in this case it is also returning simple integer variables. I don't understand why this happens.
> A function with a type of int& should return reference variables.
> But in this case it is also returning simple integer variables.

It is returning a reference to int.

The semantics of return involves the semantics of initialization.

1
2
3
4
5
6
7
8
9
10
11
double foo()
{
     int i = 1234 ;
     return i+3 ; // return a double initialized with the expression i+3
}

int& bar()
{
    static int i = 1234 ;
    return i ; // return a reference to int, initialized to refer to i  
}

1
2
3
4
5
int& bar()
{
    static int i = 1234 ;
    return i ; // return a reference to int, initialized to refer to i  
}


The comment says that initialized to refer to i
How and why does this happen? Also, I didn't understand what you mean when you wrote The semantics of return involves the semantics of initialization. I request you to elaborate your explanation. Thanks!
c and d (*d) are not simple integers. They are variables (of type of simple integer, perhaps).

try return 153; and you will find the difference ;)

---

search also for
lvalue vs rvalue
When I wrote return 153;, I'm getting this:

error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
Last edited on
so you should get it, right?

if not so, maybe one more hint>
do not read reference variable as in
c isn't a reference variable

read reference to a variable instead:

So int & returnC() is expected to return a reference to a variable, which is exactly what it does with return c and which is exactly what it cannot do with return 153;
Last edited on
@PavDub I see ... Thanks for the explanation. I think I almost get it but ...

Is there a significant or rather any "worthy to note" difference between the two functions?

1
2
3
4
5
6
7
int& returnC()
{
    static int c;
    int &d = c;
    c=10;
    return d;
}


and

1
2
3
4
5
6
int& returnC()
{
    static int c;
    c=10;
    return c;
}
Last edited on
> Is there a significant or rather any "worthy to note" difference between the two functions?

No. In practice, the optimizer will elide d in the first function.
(d is an alias for c ; return d ; is equivalent to return c ;)
Topic archived. No new replies allowed.