简体   繁体   English

该程序中“&”和有什么区别

[英]What's the difference between “&” in this program

Below is part of a C++ program: 以下是C ++程序的一部分:

Circle circle1, &circle2 = circle1, *p = &circle2;

I'm wondering what is the difference in between the two & s there? 我不知道是什么在两者之间的区别&在那儿? Thanks so much. 非常感谢。

第一(使用& )的声明一个圆参考,后者是用于获得的存储器地址的地址的操作者circle2

Circle circle1, &circle2 = circle1, *p = &circle2;

is equivalent to: 等效于:

Circle circle1;
Circle &circle2 = circle1;  // & is used to declare a reference variable
Circle *p = &circle2;       // & is used to take the address of circle2

They have two very different meanings. 它们有两个非常不同的含义。 Its easier to see if you split up the terms. 如果您拆分条款,则更容易看到。

Circle circle1; // A Circle object

Circle& circle2 = circle1; // A Circle reference

Circle* p = &circle2; // A Circle pointer takes the address of a Circle object

In the second line you are declaring a reference to a Circle . 在第二行中,您声明对Circle引用

In the third line you are taking the address of a Circle . 在第三行中,您使用的是Circle地址

So the second line is using & to declare a reference type . 因此,第二行使用&声明引用类型

The third line is using & as the address of operator. 第三行使用&作为运算符的地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM