+ 4

How is this working fine?

How is my below program working fine??? https://code.sololearn.com/cWbo3mPUh4Ho/?ref=app

7th Dec 2021, 11:15 AM
Rishi
Rishi - avatar
3 Answers
+ 2
The code will break as soon as you dereference (and use) 'this' (nullptr) That means accessing the variable '_i', making the method virtual, or simply doing '*this' The reason it works is because under the hood you're calling Foo::f(p) And while you do dereference nullptr doing p->, you're not actually "using" it
7th Dec 2021, 12:02 PM
Angelo
Angelo - avatar
+ 2
Angelo I understand that in my case I'm not dereferencing the NULL pointer. But how come the compiler doesn't notice it? Or is there some kind of "hack" going on here?
7th Dec 2021, 2:45 PM
Rishi
Rishi - avatar
+ 2
Rishi the compiler won't notice because it's not its job What the nullptr is pointing to is indeed a Foo object, because you said it is
7th Dec 2021, 3:15 PM
Angelo
Angelo - avatar