views:

235

answers:

6

Can someone give me a brief definition of a reference variable in C++?

+1  A: 

Reference variables allow two variable names to address the same memory location:

main()
{
    int var1;
    int &var2 = var1;       // var2 is a reference variable.

    var1 = 10;

    cout << "var1 = " << var1 << endl;
    cout << "var2 = " << var2 << endl;
}

Resource: LINK

Jonathan
+1  A: 

Google is your friend... Anyway is a variable which references another one:

int foo;
int& bar = foo;

Bar is now a reference, which is to say that bar holds the location of memory where foo lies.

Enrico Carlesso
Technically not. If bar was a variable you could get its address. A reference is an alias to another variable (not the address of as this would imply the compiler would need to insert a dereference operation). When this gets compiled out bar probably is just replaced by foo.
Martin York
+5  A: 

Reference variable is an alias (an alternate name) for an object. [From C++ FAQ ]

aJ
Great faq, "Use references when you can, and pointers when you have to."
Hernán Eche
+1  A: 

The first paragraph of the Wikipedia article could easily serve as a brief definition:

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C.

And quoting from the same article:

C++ references differ from pointers in several essential ways:

  • It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

  • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

  • References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.

  • References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Further reading:

Daniel Vassallo
Wikipedia is not a very authoritative source.
Martin York
+7  A: 

A reference is an entity that is an alias for another object.

A reference is not a variable as a variable is only introduced by the declaration of an object. An object is a region of storage and, in C++, references do not (necessarily) take up any storage.

As objects and references are distinct groups of entities in C++ so the term "reference variable" isn't meaningful.

Charles Bailey
Well, not *yet* a variable. Good that C++0x fixes that - it's been a long standing issue in the Standard. See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#633 and take a look into n3090, it's already been incorperated into the FCD. Anyway, to be complete, you could mention that references can refer to functions too. +1 for technical accuracy :)
Johannes Schaub - litb
A: 

A reference is, pretty literally, *ptr, with the behaviours you'd expect from such a thing.

DeadMG
No its not. Try taking its address, re-assigning it, assigning via it de-referencing it. In the simple case it is probably not even implemented as a pointer.
Martin York