Reference (C++)

Reference (C++)

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. The name "C++ reference" may cause confusion, as in computer science a reference is a general concept datatype, with "pointers" and "C++ references" being specific reference datatype implementations.

Syntax and terminology

The declaration of the form:

&

where is a type and is an identifier is said to define an identifier whose type is reference to .

Examples:
# int A = 5;
# int& rA = A;
# extern int& rB;
# int& foo ();
# void bar (int& rP);
# class MyClass { int& m_b; /* ... */ };
# int funcX() { return 42 ; }; int (&xFunc)() = funcX;

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, funcX() is a function that returns an int, xFunc() is an alias for funcX.

Types which are of kind "reference to " are sometimes called reference types. Identifiers which are of reference type are called reference variables. To call them "variable", however, is in fact a misnomer, as we will see.

Relationship to pointers

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.
* As a consequence of the first point, neither arithmetic, casts, nor any other operation can be performed on references except copying their binding into other references.
* Once a reference is created, it cannot be later made to reference another object; we say 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.

e.g

int& k; // compiler will complain: error: `k' declared as reference but not initialized

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.

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 much easier to reason than pointers and are more resistant to errors. 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:
* If it refers to an object with automatic allocation which goes out of scope,
* If it refers to an object inside a block of dynamic memory which has been freed.The first is easy to detect automatically if the reference has static scoping, but is still a problem if the reference is a member of a dynamically allocated object; the second is more difficult to assure. These are the only concern with references, and are suitably addressed by a reasonable allocation policy.

Uses of references

* 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: void square(int x, int& result) { result = x * x; }

Then, the following call would place 9 in "y":square(3, y);

However, the following 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 = 5

* In 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 xf_fast(y); // fast, gives direct read-only access to yIf f_slow() 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

References are defined by the ISO C++ standard as follows (excluding the example section): epigraph
quote = In a declaration T D where D has the form& D1and the type of the identifier in the declaration T D1 is “"derived-declarator-type-list" 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 (const and "volatile") 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": intypedef int& A;const A aref = 3; // ill-formed;// non-const reference initialized with rvalue

the type of 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.

It is unspecified whether or not a reference requires storage (3.7).

There shall be no references to references, no arrays of references, and no pointers to references. The declaration of a reference shall contain an "initializer" (8.5.3) except when the declaration contains an explicit 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. ]
cite = ISO/IEC 14882:1998(E), the ISO C++ standard, in section 8.3.2 [dcl.ref]

External links

* [http://www.parashift.com/c++-faq-lite/references.html References in the C++ FAQ Lite]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • référence — [ referɑ̃s ] n. f. • v. 1820; angl. reference, même o. que référer I ♦ 1 ♦ Action ou moyen de se référer, de situer par rapport à. Indemnité fixée par référence au traitement. Géom. Système de référence : système d axes et de points par rapport… …   Encyclopédie Universelle

  • reference — ref‧er‧ence [ˈrefrəns] noun [countable] 1. with reference to formal used to say what you are writing or talking about, especially in business letters: • With reference to your recent advertisement, I am writing to apply for the post of sales… …   Financial and business terms

  • Reference — Référence Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom …   Wikipédia en Français

  • reference — ref·er·ence / re frəns, fə rəns/ n 1: an act of referring; specif: mention or citation of one document (as a statute) in another a municipality may adopt by reference all or a part of this title Alaska Statutes see also incorporate 2 …   Law dictionary

  • Reference — Ref er*ence (r?f ?r ens), n. [See {Refer}.] 1. The act of referring, or the state of being referred; as, reference to a chart for guidance. [1913 Webster] 2. That which refers to something; a specific direction of the attention; as, a reference… …   The Collaborative International Dictionary of English

  • reference — [ref′ə rəns, ref′rəns] n. 1. a referring or being referred; esp., submission of a problem, dispute, etc. to a person, committee, or authority for settlement 2. relation; connection; regard [in reference to his letter] 3. a) the directing of… …   English World dictionary

  • reference — ► NOUN 1) the action of referring to something. 2) a mention or citation of a source of information in a book or article. 3) a letter from a previous employer testifying to someone s ability or reliability, used when applying for a new job. ►… …   English terms dictionary

  • reference — see IDEA OF REFERENCE ref·er·ence ref (ə )rən(t)s adj of known potency and used as a standard in the biological assay of a sample of the same drug of unknown strength <a dose of reference cod liver oil> …   Medical dictionary

  • reference — [n1] remark, citation advertence, allusion, associating, attributing, bringing up, connecting, hint, implication, indicating, innuendo, insinuation, mention, mentioning, note, plug*, pointing out, quotation, relating, resource, source, stating;… …   New thesaurus

  • reference — testimonial, recommendation, character, *credential …   New Dictionary of Synonyms

  • Reference — For help in citing references, see Wikipedia:Citing sources. For the Wikipedia Reference Desk, see Wikipedia:Reference desk. Reference is derived from Middle English referren, from Middle French rèférer, from Latin referre, to carry back , formed …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”