Reference (C Plus Plus) Guide, Meaning , Facts, Information and Description
In C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C, which is a reference in the general sense but not in the sense used by C++.
| Table of contents |
|
2 Relationship to pointers 3 Uses of references 4 Quotes 5 References |
The declaration of the form
Examples:
Types which are of kind "reference to
C++ references differ from pointers in several essential ways:
A consequence of this is that in many implementations, operating on a variable with automatic or static lifetime through a reference, although syntactically similar to accessing it directly, can involve hidden dereference operations that are costly.
Also, because the operations on references are so limited, they are also much easier to reason about formally than pointers, and harder to cause errors with. While pointers can be made invalid through a variety of mechanisms, ranging from carrying a null value to out-of-bounds arithmetic to illegal casts to producing them from random integers, a reference only becomes invalid in two cases:
Other than just a helpful replacement for pointers, one convenient application of references is in function parameter lists, where they allow passing of parameters used for output with no explicit address-taking by the caller. For example:
References are defined by ISO/IEC 14882:1998(E), the ISO C++ standard, in section 8.3.2 [dcl.ref], as follows (excluding the example section):
This is an Article on Reference (C Plus Plus). Page Contains Information, Facts Details or Explanation Guide About Reference (C Plus Plus) Syntax and Terminology
where
Here, rA and rB are of type "reference to int", foo() is a function that returns a reference to int, bar() is a function with a reference parameter, which is reference to int, MyClass is a class with a member which is reference to int. int anA = 5;
int A;
int& rA = A;
extern int& rB;
int& foo ();
void bar (int& rP);
class MyClass { int& m_b; /* ... */ };Relationship to pointers
There is a simple conversion between pointers and references: the address-of operator (&) will yield a pointer referring to the same object when applied to a reference, and a reference which is initialized from the dereference of a pointer value will refer to the same object as that pointer, where this is possible without invoking undefined behavior. This equivalence is a reflection of the typical implementation, which effectively compiles references into pointers which are implicitly dereferenced at each use.
The first is easy to detect automatically due to static scoping of variables; the second is more difficult to assure, but it is the only concern with references, and one suitably addressed by a reasonable allocation policy.Uses of references
void square(int x, int& result) {
result = x*x;
}
Then, this call would place 9 in y:
square(3, y);
However, this call would give a compiler error, since reference parameters not qualified with const can only be bound to addressable values:
square(3, 6);
Returning a reference also allows a surprising syntax in which function calls can be assigned to:
int& preinc(int& x) { ++x; return x; }
preinc(y) = 5; ''// same as ++y, y = 5In many implementations, normal parameter-passing mechanisms often imply an expensive copy operation for large parameters. References qualified with const are a useful way of passing large objects between functions that avoids this overhead:
void f_slow(BigObject x) { /* ... */ }
void f_fast(const BigObject& x) { /* ... */ }
BigObject y;
f_slow(y); ''// slow, copies y to parameter x
f_fast(y); ''// fast, gives direct read-only access to y
If f_fast() actually requires its own copy of x that it can modify, it must create a copy explicitly. While the same technique could be applied using pointers, this would involve modifying every call site of the function to add cumbersome address-of (&) operators to the argument, and would be equally difficult to undo, if the object became smaller later on.Quotes
& D1
T,” then the type of the identifier of D is “derived-declarator-type-list reference to T.” Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef (7.1.3) or of a template type argument (14.3), in which case the cv-qualifiers are ignored. [Example: in
typedef int& A;
const A aref = 3; // ill-formed;
// non-const reference initialized with rvalue
aref is “reference to int”, not “const reference to int”. ] [Note: a reference can be thought of as a name of an object. ] A declarator that specifies the type “reference to cv void” is ill-formed.extern specifier (7.1.1), is a class member (9.2) declaration within a class declaration, or is the declaration of a parameter or a return type (8.3.5); see 3.1. A reference shall be initialized to refer to a valid object or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior. As described in 9.6, a reference cannot be bound directly to a bitfield. ]References
