//C++的引用类型: //这里理解为别名 //1.基本数据类型的引用:& #include <iostream> using namespace std; int main() { int a = 3; int &b = a; //引用类型必须初始化 b = 10; cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; return 0; }
如图:
//2.结构体类型的相关引用 #include <iostream> using namespace std; typedef struct { int x; int y; }Coor; int main() { Coor c1; Coor &c = c1; c.x = 10; c.y = 20; cout<<"应用后x的值是:"<<c1.x<<endl; cout<<"应用后y的值是:"<<c1.y<<endl; return 0; }
如图:
//3.指针类型的引用 //类型 *&别名 = 指针 #include <iostream> using namespace std; int main() { int a = 10; int *p = &a; int *&q = p; *q = 20; cout<<"a的值是: "<<a<<endl; return 0; }
如图:
时间: 2024-10-01 06:14:58