FAQ 11.03 When can a reference be attached to its referent?

A reference can be attached to its referent only at the moment the reference is initialized. Not only that, C++ requires every reference initialization to have a referent.

The following example initializes j to be an alias for i, but the initialization of k is illegal because reference k is not attached to an object.

 int main() {   int  i;   int& j = i;   #ifdef GENERATE_ERROR     int& k;   //ERROR: references must be initialized   #endif } 

When a function receives a parameter by reference, the reference is initialized by being attached (bound) to the actual argument provided by the caller.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net