Operator "&"

I have a question about the "&" operator.
I know that it's an address operator, that &var1 gives the address of var1.
My question is what is the use of "&" after the type name, in a declaration?
I was reading tutorials about SFML and came across this line of code
const sf::Input& Input = App.GetInput();
And I don't know what the "&" does.
Last edited on
It is declaring a const reference to an instance of sf::Input and assigning it whatever value is returned from App.GetInput(). More simplistic:

1
2
3
4
5
6
7
8
9
10
11
class foo
{
  private:
    int x;
  public:
    foo(int i) : x(i) {}
    int getx() { return x; }
};

foo myfoo(12);
const int &myfoox = myfoo.getx();

Last edited on
closed account (zb0S216C)
When an ampersand (&) is used in a declaration, just like Input, it means you're declaring a reference. References are types that behave like synonyms for existing variables/objects. The variable/object that it's bound to is known as a referent, and cannot be redirected once bound.

This reference declaration:

 
int const &r_(32);

...is known as a R-Value reference, which basically means it's a reference to a variable/object who's state cannot change. However, R-Value reference has become ambiguous since the release of C++11's R-Value reference:

 
int &&r_(12); // R-Value reference.  

This reference declaration:

1
2
int a_(0);
int &r_(a_);

...is known as a L-Value reference, which means it's a reference to a variable/object who's state can change.

For more information, see here: http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

Wazzak
Last edited on
Hmmm, I think I understand..
So essentially it's like a compiler-managed pointer?

Texan40, if I understand correctly, App.GetInput() returns an object, an instance of the class sf::Input?

Framework, what do you mean by "cannot be redirected once bound"?
Does it mean I can't do this?
1
2
3
4
int a = 5;
int b = 10;
int& c = a;
c = b;

If so, then it's rather different to a pointer, as non-constant pointers can be reassigned.

PS : I received an email notification that someone with the username "Moschops" replied my post, but why doesn't it show up on this thread?
Last edited on
So essentially it's like a compiler-managed pointer?


Please don't think of it like that. It is true that many compilers deal with it by having a pointer hanging around behind the scenes, but that's just how they happen to implement it. It's not a good way to think about it.

A reference is another name for the exact same object.

1
2
3
4
5
6
7
8
9
10
11
int a = 5;
int b = 10;
int& c = a; // c is another name for a
// At this point, the value of c is 5, and the value of a is 5, because
//  they are the exact same object. Not a copy of each other. There is only one object.
c = b; // remember that c is another name for a
// At this point, the value of c is 10, and the value of a is 10
b = 20;
// // At this point, the value of c is 10, and the value of a is 10
// changing the value of b had no effect. 
// c and a are the same object. Changing one is changing the other. 


"cannot be redirected once bound"?

In the above code, c is another name for a. You cannot change that; you cannot come back later and say that it's now going to be another name for b.
Last edited on
Okay then another name for the same object, and it can't be changed.

I'll do some more searching for pass by reference, as you told me. I didn't know what it was called.

Thanks very much for the help, everyone.
Topic archived. No new replies allowed.