Reference (C++) - Syntax and Terminology

Syntax and Terminology

The declaration of the form:

&

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

Examples:

  1. int A = 5;
  2. int& rA = A;
  3. extern int& rB;
  4. int& foo ;
  5. void bar (int& rP);
  6. class MyClass { int& m_b; /* ... */ };
  7. 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 a "reference to int". MyClass is a class with a member which is reference to int; funcX is a function that returns a (non-reference type) int, and 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.

Read more about this topic:  Reference (C++)

Famous quotes containing the words syntax and:

    Syntax and vocabulary are overwhelming constraints—the rules that run us. Language is using us to talk—we think we’re using the language, but language is doing the thinking, we’re its slavish agents.
    Harry Mathews (b. 1930)