// references1.cpp By: Aiman Hanna - ©1993-2004 Aiman Hanna // This programs illustrates reference and gives examples of returning // by value or returning by reference. In addition it shows // some of the illegal manipulations/uses of references. #include void ReferencMeaning( void ) { // This function explains the meaning of a reference. // A reference serves as an alias for another variable, // it is implemented using the & operator. int Count = 12; int &RefCount = Count; // A reference has to be initialized // to a variable. Now both Count and // RefCount refer to the same memory // location. cout << " The value of Count is: " << Count << endl; cout << " The value of RefCount is: " << RefCount << endl; RefCount = 5; cout << " The value of Count is: " << Count << endl; cout << " The value of RefCount is: " << RefCount << endl; Count--; cout << " The value of Count is: " << Count << endl; cout << " The value of RefCount is: " << RefCount << endl; } /* This function show examples of illegal references. Note that it is all commented out. Uncomment this part to see the problems........... void IllegalReferences( void ) { // This function shows examples of illegal references.. // better not to run it!! Well, give it a try if you // are curious! int Count1, Count2; int &IllegalRef1; // A reference must be initialized // when created. It can not be redefined then. int &LegalRfef1 = Count1; int &IllegalRef2 = Count2; // So far is okay. the following is not. int &IllegalRef2 = Count1; // Regardless the re-declaration illegality, // once initialized, a reference can not refer // to another variable. int &IllegalRef3 = 5; // IllegalRef3 is not a const reference. const int &LegalRef3 = 5; // Okay as LegalRef3 is a const reference. const int c1 = 10; const int &LegalRef4 = c1; // This is allowed, however LegalRef4 // can not be used to modify the value // of c1. const int &IllegalRef4 = c1; // So far is okay. The following is not. IllegalRef4 = 12; // Can not modify the value of c1. int v1; // v1 is not a constant. const int &Legal5 = v1; // This is still allowed. However, // still can not modify the value of v1. // Here v1 can modify itself. The reference // will behave as Read-Only for v1. v1 = 10; // Okay. const int &IllegalRef5 = v1; // So far is okay. The following is not. IllegalRef5 = 12; // Can not modify the value of v1. int &const LegalRef6 = c1; // LegalRef6 is itself constant. This is Legal // but senseless. Why? Remember that once a // reference is initialized, you can not make // it refer to another variable anyways. // See IllegalRef2 above. } */ // Remove this comments and call the function if you want to test it. int* fun1() { int* p = new int; *p = 1; return p; } int& fun2() { int* p = new int; *p = 2; return *p; } int& fun3() { int x; int* p = &x; *p = 3; return *p; } int& fun4() { int x; int* p = &x; *p = 4; return x; // warning C4172: returning address of local variable or temporary } int& fun5() { int x = 5; return x; //warning C4172: returning address of local variable or temporary } int fun6() { int x = 6; return x; //no problem here } int& fun7() // this is exactly the same like fun5() { int x = 7; return x; //warning C4172: returning address of local variable or temporary } int& fun8() { static int x = 8; return x; } int main() { ReferencMeaning(); int* q = fun1(); cout << "*q is: " << *q << endl; cout << "fun2 returns " << fun2() << endl; int* p21 = &fun2(); cout << " *p21 is " << *p21 << endl; int* p22 = &fun2(); cout << " *p22 is " << *p22 << endl; (*p22) *=10; cout << " *p21 now is " << *p21 << endl; cout << " *p22 now is " << *p22 << endl; cout << "fun3 returns " << fun3() << endl; int* p3 = &fun3(); cout << " *p3 is " << *p3 << endl; cout << " *p3 once again is " << *p3 << endl; int y = fun3(); cout << " y is " << y << endl; cout << " y once again is " << y << endl; cout << "fun4 returns " << fun4() << endl; y = fun4(); cout << " y is " << y << endl; cout << "fun5 returns " << fun5() << endl; y = fun5(); cout << " y is " << y << endl; cout << "fun6 returns " << fun6() << endl; y = fun6(); cout << " y is " << y << endl; // fun6() = 66; // error C2106: '=' : left operand must be l-value fun7() = 77; cout << "fun7 is: " << ++fun7() << endl; cout << "fun7 again is: " << ++fun7() << endl; cout << "fun7 again and again is: " << ++fun7() << endl; fun8() = 88; cout << "fun8 is: " << ++fun8() << endl; cout << "fun8 again is: " << ++fun8() << endl; cout << "fun8 again and again is: " << ++fun8() << endl; return 0; } /* The Output The value of Count is: 12 The value of RefCount is: 12 The value of Count is: 5 The value of RefCount is: 5 The value of Count is: 4 The value of RefCount is: 4 *q is: 1 fun2 returns 2 *p21 is 2 *p22 is 2 *p21 now is 2 *p22 now is 20 fun3 returns 3 *p3 is 3 *p3 once again is 1245056 y is 3 y once again is 3 fun4 returns 4 y is 4 fun5 returns 5 y is 5 fun6 returns 6 y is 6 fun7 is: 8 fun7 again is: 8 fun7 again and again is: 8 fun8 is: 89 fun8 again is: 90 fun8 again and again is: 91 */