- #include
- using namespace std;
- int main( )
- {
- int a ;
- int &b = a; //b和a实际上是同一变量
- b = 100; //b赋值为100,也就是a赋值为100
- cout << a << endl;
- //a的地址和b的地址应该是完全一样
- cout << &a << endl;
- cout << &b << endl;
- /*-------▼下面代码有错,注释后才能运行▼-----------
- int x[] = {12,15,20} ;
- int &y = x; //错误:数组、指针不能被引用
- int x1;
- int &y1; //错误:引用时必须初始化
- y1 = x1;
- -------▲上面代码有错,注释后才能运行▲-----------*/
- return 0;
- }
#include运行结果:using namespace std; //|右边为指针作参数的代码,仅作比较用。 void swap(int &x, int &y); //|void swap(int *x, int *y); //| int main ( ) //| { //| int i = 12; //| int j = 25; //| cout << "i=" << i << " j=" << j << endl; //| //| swap(i, j); //|swap(&i, &j); cout << "i=" << i << " j=" << j << endl; //| //| return 0; //| } //| //| void swap(int &x, int &y) //|void swap(int *x, int *y) { //|{ int t; //| int t; //| t = x; //| t = *x; x = y; //| *x = *y; y = t; //| *y = *x; } //|}
#include运行结果:using namespace std; class myclass { int who; public: myclass(int n) { who = n; cout << "构造函数调用" << who << endl; } ~myclass() { cout << "析构函数调用" << who << endl; } int id() { return who; } }; void f1(myclass o) { //普通变量方式传递参数 cout << "外部函数调用" << o.id() << endl; } void f2(myclass *o) { //指针方式传递参数 cout << "外部函数调用" << o->id() << endl; } void f3(myclass &o) { //引用方式传递参数 cout << "外部函数调用" << o.id() << endl; } int main ( ) { myclass x1(1); f1(x1); cout << "-------" << endl; myclass x2(2); f2(&x2); cout << "-------" << endl; myclass x3(3); f3(x3); cout << "-------" << endl; return 0; }

#include许多人都认为第9句错了,少数人认为第4句可能有问题。事实上,这个程序完全正确。using namespace std; int &f(); int x; int main ( ) { f() = 100; cout << x << endl; return 0; } int &f() { return x; }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30110178/viewspace-1407051/ ,如需转载,请注明出处,否则将追究法律责任。