Lecture overview -- Keyboard shortcut: 'u'  Previous page: References - Examples -- Keyboard shortcut: 'p'  Next page: References versus Pointers -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home    Basic facilities - slide 18 : 42

Constant References

A constant reference is really a reference to a constant

"An initializer for const T&   does not need to be an lvalue, or even of type T."

  const T &var = expression;
const-ref-t-1.cc
The initialization of var in a real, but simple context.
const-ref-t-3.cc
A similar program that initializes a user defined struct via a 'functional casting' constructor.
ref-div.cc
An envelope around the stdlib div function that returns a struct of quotient and remainder.
Go to exercise
Ways of returning results from the div function
const-ref-t-2.cc
A similar setup - illustrates that it is not good to return a reference to a deallocated local variable.