Refrence

Hello
I can't understand functon refrences
can somebody say difrences btween this code


#include "stdafx.h"
#include <iostream>
using namespace std;
int & r(int a)
{
return a;
}

int main()
{
int b=3;
cout<<r(b)<<endl;
}

And this code

#include "stdafx.h"
#include <iostream>
using namespace std;
int r(int a)
{
return a;
}

int main()
{
int b=3;
cout<<r(b)<<endl;
}
I think the top function should be this:

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

When it has an "&" it's a pass by reference. That means the changes made to "a" in the function will be permanent. When it doesn't have an "&", the compiler uses a copy of "a" and the changes to "a" in the function aren't permanent in main().
but there is no errors and both of them returns 3
closed account (z05DSL3A)
Having a reference returned means that the function can be used on the left hand side of an asignment operator (it is an lvalue). this dose not make sense in your example but constider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Array {
 public:
   int size() const;
   float& operator[] (int index);
   ...
 };
 
 int main()
 {
   Array a;
   for (int i = 0; i < a.size(); ++i)
     a[i] = 7;    // This line invokes Array::operator[](int)
   ...
 }


If operator[] did not return a reference, then you could not call Array::operator[](int) in an lvalue position.
I don't no operators(becuse i didn't rich to it in my book) can you say me an example without operator!
isn't any one yo help me
closed account (z05DSL3A)
From MSDN:
Reference-Type Function Returns

Functions can be declared to return a reference type. There are two reasons to make such a declaration:

• The information being returned is a large enough object that returning a reference is more efficient than returning a copy.

• The type of the function must be an l-value.

Just as it can be more efficient to pass large objects to functions by reference, it also can be more efficient to return large objects from functions by reference. Reference-return protocol eliminates the necessity of copying the object to a temporary location prior to returning.

Reference-return types can also be useful when the function must evaluate to an l-value. Most overloaded operators fall into this category, particularly the assignment operator.

Consider the Point example:
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
32
33
34
35
36
37
38
// refType_function_returns.cpp
// compile with: /EHsc

#include <iostream>
using namespace std;

class Point
{
public:
    // Define "accessor" functions as
    //  reference types.
    unsigned& x();
    unsigned& y();
private:
    unsigned obj_x;
    unsigned obj_y;
};

unsigned& Point :: x()
{
    return obj_x;
}
unsigned& Point :: y()
{
    return obj_y;
}

int main()
{
    Point ThePoint;
    // Use x() and y() as l-values.
    ThePoint.x() = 7;
    ThePoint.y() = 9;

    // Use x() and y() as r-values.
    cout << "x = " << ThePoint.x() << "\n"
            << "y = " << ThePoint.y() << "\n";
}


Notice that the functions x and y are declared as returning reference types. These functions can be used on either side of an assignment statement.

Declarations of reference types must contain initializers except in the following cases:

• Explicit extern declaration

• Declaration of a class member

• Declaration within a class

• Declaration of an argument to a function or the return type for a function



Last edited on
Thanks alot
Topic archived. No new replies allowed.