3 ms·
All it takes is X& x = *p; // p might be 0. Y& y = p->y; It is the responsibility of whoever made the reference not to dereference a null pointer when in
by ncmncm 5y ago
All it takes is
X& x = *p; // p might be 0.
Y& y = p->y;
It is the responsibility of whoever made the reference not to dereference a null pointer when initializing it. In the second form, it wouldn't help if you could check for a null &y, because it (probably) wouldn't be null anyway.
- tialaramex 5y agoRight but the post you're replying to asked for any way to do this without invoking Undefined Behaviour. If p is a null pointer, dereferencing it is Undefined Behaviour and you lose immediately.