References are declared using the declarator syntax:
Syntax
& cv-qualifier-listopt dname
A reference holds the address of an object but behaves syntactically like an object. A reference declaration consists of an (optional) list of specifiers followed by a reference declarator.
Syntax
decl-specifiers & cv-qualifier-listopt dname ;
Consider the user-defined type Date
:
struct Date
{
short DayOfWeek;
short Month;
short Day;
short Year;
};
The following statements declare an object of type Date
and a reference to that object:
Date Today; // Declare the object.
Date& TodayRef = Today; // Declare the reference.
The name of the object, Today
, and the reference to the object, TodayRef
, can be used identically in programs:
Today.DayOfWeek = 3; // Tuesday
TodayRef.Month = 7; // July