The output of this piece of code is
21
I am unable to explain it to myself. My thinking (what I learned) is that an implicit copy constructor is called (triggered by the assignment) and all elements are copied bit by bit. However this does not apply to external elements, like the private variable val
, which is a pointer. So b
has a different pointer, but it is pointing to the same element.
So I expected the result to be
22
That is .get()
is called twice by a
and b
and thus the dereferenced pointer value should be 1
(after the first get()
) and then 2
(after the second get()
). Finally the values are written to the console output.
C++ code:
#include <iostream>
using namespace std;
class A {
int *val;
public:
A() {
val = new int;
*val = 0;
}
int get() {
return ++(*val);
}
};
int main() {
A a, b = a;
cout << a.get() << b.get();
return 0;
}
This is what the code actually does:
or
(I can't get the latter to compile due to overloading issues, but it does happen behind the scenes)
Which means that the code evaluates std::endl before b.get(), and b.get() before a.get(). So b.get() increases val's pointed value by 1 and then returns 1, and a.get() increases val's pointed value by 1 and returns 2. The operators are called in the expected order after the parameters are evaluated. I'd guess this is compiler dependent, but I ran your code and got the 21 as well.
In this case you are using an implicit copy constructor. What it does is copy the memory of your instance a to instance b. (As you said, byte by byte)
So b now owns its own pointer which holds the same value as a's pointer, that is to the same address. Since you increase the value on get(), the returned value would never be the same so you would never get 22 as a result.
Edit: You didn't actually use std::endl, it's my habit.