1

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.

Barry
  • 286,269
  • 29
  • 621
  • 977
Enkri_
  • 161
  • 2
  • 12

3 Answers3

6

The first (use of &) is declaring a Circle reference and the latter is the address-of operator for obtaining the memory address of circle2.

Tas
  • 7,023
  • 3
  • 36
  • 51
4
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
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

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.

Galik
  • 47,303
  • 4
  • 80
  • 117