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.
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.
The first (use of &
) is declaring a Circle reference and the latter is the address-of operator for obtaining the memory address of 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.