Site Tools

User Tools


References

See web reference links at the bottom for more information

Does the ampersand always come after the data type?

  • According to the C++ Pocket Reference the ampersand precedes the variable name in a declaration.
  • References are used to provide alternative names for the variables. They are declared by placing an ampersand (&) before the variable name.
  • Page 341
int rats;
 
// means "reference-to-int"
int & rodents = rats;
  • Page 275
int &count;
  • Page 277
void squareByReference(int &numberRef)
{
    numberRef  *= numberRef;
}

Some C++ programmers prefer to write:

int& numberRef

Conclusion

To me, this indicates that the placement of the & can be chosen:

  • Next to the data type
  • Next to the variable

Web References