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
  • 267,863
  • 28
  • 545
  • 906
Enkri_
  • 151
  • 2
  • 10

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
  • 6,793
  • 3
  • 34
  • 49
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
  • 200,579
  • 13
  • 144
  • 260
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
  • 44,976
  • 4
  • 80
  • 109