Export (0) Print
Expand All

References

References are declared using the declarator syntax:

decl-specifiers & cv-qualifier-listopt dname

A reference holds the address of an object, but behaves syntactically like an object. A reference declaration consists of a (optional) list of specifiers followed by a reference declarator.

In the following program, notice that the name of the object, Today, and the reference to the object, TodayRef, can be used identically in programs:

// references_ovvw.doc
#include <stdio.h>
struct S
{
   short i;
};

int main()
{
   S  s;   // Declare the object.
   S& SRef = s;   // Declare the reference.
   s.i = 3;
   printf("%d\n", s.i);
   printf("%d\n", SRef.i);
   SRef.i = 4;
   printf("%d\n", s.i);
   printf("%d\n", SRef.i);
}

Output

3
3
4
4

Topics in this section:

See Also

Initializing References

Show:
© 2014 Microsoft