简体   繁体   中英

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

Below is part of a C++ program:

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 .

In the third line you are taking the address of a Circle .

So the second line is using & to declare a reference type .

The third line is using & as the address of operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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