So the conclusion is that the value of b is assigned to a?
*initially*
What happens after "initially"?
I edited.
All right, let's ask the compiler again.
int main()
{
int *a, *b;
a = b = new int;
std::cout << "value of a is " << a << std::endl;
std::cout << "value of b is " << b << std::endl;
std::cout << "address of b is " << &b << std::endl;
b = new int;
std::cout << "value of a is " << a << std::endl;
std::cout << "value of b is " << b << std::endl;
std::cout << "address of b is " << &b << std::endl;
}
a = b = new c creates (allocates) one object of type c, then assigns the pointer to b, and then assigns the same pointer to a.
Careful, that isn't actually true.
a will point to b, while b will point to c. The fact that b currently points to c wont hold true if b is reassigned.
... I think I said that poorly.
http://objection.mrdictionary.net/go.php?n=6774974
Actually that captures it perfectly ( and is completely wrong! ;) ).
The address of b is assigned to a, not the value. This is exactly what I was warning of.
No. First, a = b = new c is -- regardless of types or whether they're values or pointers --- equivalent to a = (b = new c). It means assign return value of "create c", to b, then assign b to a, not something different, ever. That's how the language is defined.
new c returns a pointer to the newly created object of type c (or throws), which necessarily means that b must be of type c* (pointer-to-c) or at least a compatible pointer (e.g. base class of c), or well, a compatible smart pointer. Otherwise this will not compile.
This, by consequence, means that a also has to be of type c*, because you cannot assign a pointer to "something else", say, an integer, or an object (not without a cast, a conversion operator, or a compile error, anyway), and a therefore also points to c (which is the value [of pointer-type] of b).
a does not point to b. It is assigned b's value (not its address!), which is a pointer to c.
Yes, you could of course be extra smart and define a as a class that has a constructor taking a pointer to type c, but that's a sophistry. It is technically possible to abuse the language in a way which makes this possible, but meh.
No, we are saying the same thing I am just doing a poor job of it.
a = b = new c()
The b points at the newly allocated memory address, while a points at b, so they effectively have the same value. It does not create two new memory allocations like a new developer might expect, it creates one and points b at that address, then points a at b which ultimately points a c. If you change b to point at something else, a will point at something else as well, because a is pointing at the address of b, not the original value c like some might expect. The above line of code creates a single memory allocation. If you change what b points at, you change was a points at, as a points at b, not c. This is where I think a new developer will get hung up.
So,
int sample = 42;
int sample2 = 43;
int *a;
int *b;
a = b = &sample;
cout << a; // 42
b = &sample2;
cout << a; //43;