Objects in the heap and links

Answering this question , I was a little embarrassed. We all know that this works great thanks to C ++ copy semantics:

int *some_obj = new int(42);
int a_copy = *some_obj;

delete some_obj;

printf("The answer is %d\n", a_copy);

But what about this?

int *some_obj = new int(42);
int& a_ref = *some_obj;

delete some_obj;

printf("The answer is %d\n", a_ref);

This access to remote memory?

Different times may be given in different forms, but it is not very friendly. Damn, I could not make a worthy title.

+5
source share
2 answers

Yes it is. So this is not allowed. (You can make sure you see the difference using a class with a destructor that changes the value.)

+4
source

. - . - . . - , , . .

+1

All Articles