自考新教材--p39

源程序:

using namespace std;

int main()

{

int oneInt = 1;

int &ref = oneInt;

const int &refc = oneInt;  //定义常引用

ref = 2;  //修改ref也即修改了oneInt

cout << "oneInt=" << oneInt << "," << "ref=" << ref << endl;//oneInt=2,ref=2

cout << "refc=" << refc << endl;

oneInt = 3;

cout << "ref=" << ref << endl;

cout << "refc=" << refc << endl;

int &ref2 = ref; //ref2和ref都是oneInt的引用

cout << "ref2=" << ref2 << endl;

//refc = 5;  //错误,不能使用常引用对所引用的变量进行修改

system("pause");

return 0;

}

运行结果:

猜你喜欢

转载自www.cnblogs.com/duanqibo/p/11969536.html