www.gusucode.com > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序 > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序/code/C++例题程序/第6章/s6_14/smain6_14.cpp
//Download by http://www.NewXing.com //文件名:smain6_14.cpp //返回引用 #include <iostream> using namespace std; int &ReturnReference(int &rInt) { return rInt; } int &ReturnReference(int *piArray, int iInt) { int iOther = piArray[iInt]; return piArray[iOther]; } //测试函数。 void main() { int iOne = 10; int iTwo; iTwo = ReturnReference(iOne)*10; cout << iTwo << " " << iOne << endl; ReturnReference(iTwo) = 200; //返回引用,又由于参数也是引用,所以改变了iTwo cout << iTwo << " " << iOne << endl; int iArray[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; for (int i=0; i<10; i++) //显示原数组元素 { cout << iArray[i] << ", "; } cout << endl; int iInt = 2; ReturnReference(iArray, iInt) = 100; for (i=0; i<10; i++) //显示函数作为左值调用以后的数组元素 { cout << iArray[i] << ", "; } cin.get(); }