Programming:C++/References

From WhyAskWhy.org Wiki
Jump to: navigation, search


Does the ampersand always come after the data type?

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.


C++ Primer Plus, 5E

Page 341

int rats;
 
// means "reference-to-int"
int & rodents = rats;


C++ How to Program, 5E

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


References