The task is to calculate the output of the following source code without a computer, which I would say is "123 234 678 678"
because ref1
is a reference on the value of ptr
and in the moment where the address of val2
is assigned to this pointer, then shouldn't ref1
as well refer to its value?
int main() {
int* ptr = nullptr;
int val1 = 123, val2 {234};
ptr = &val1;
int& ref1 = *ptr;
int& ref2 = val2;
std::cout << ref1 << " ";
std::cout << ref2 << " ";
*ptr = 456; //ref1 = 456
ptr = &val2; //*ptr = 234, why isn't ref1 as well equal to 234?
*ptr = 678; //*ptr = 678, why isn't ref1 as well equal to 678?
std::cout << ref1 << " ";
std::cout << ref2 << "\n";
return EXIT_SUCCESS;
//output: 123 234 456 678
}