引用 引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空 间,它和它引用的变量共用同一块内存空间。 一个变量可以有多个引用: int main() { //一个变量可以有多个引用 int a = 10; int& b = a; int& c = a; int& d = a; return 0; } 引用相当于给这个变量取别名,在西游记中孙悟空是孙悟空,齐天大圣也是孙悟空,弼马温也是孙悟空,孙行者也是孙悟空,斗战胜佛也是孙悟空,这些别名都是在指同一个猴子。 C++中的引用就相当于在给引用实体取别名 引用在定义的时候必须初始化: 对引用不初始化是会报错的 注意:引用类型必须和引用实体是同种类型的 引用一旦引用实体,就不能再引用其他实体: int main() { int a = 10; int& b = a; int c = 20; b = c; cout << "a=" << a<<endl; cout << "b=" << b << endl; cout << "c=" << c << endl; return 0; }...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Get in touch with Struct
Contact Struct, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack