home *** CD-ROM | disk | FTP | other *** search
- LISTING 8 - Returns an object from a function by reference
-
- //retref.cpp: Returning a reference
- #include <stdio.h>
-
- int & current(); // Returns a reference
-
- int a[4] = {0,1,2,3};
- int index = 0;
-
- main()
- {
- current() = 10;
- index = 3;
- current() = 20;
- for (int i = 0; i < 4; ++i)
- printf("%d ",a[i]);
- putchar('\n');
- return 0;
- }
-
- int & current()
- {
- return a[index];
- }
-
- // Output:
- 10 1 2 20
-
-