+ 4
How is this working fine?
How is my below program working fine??? https://code.sololearn.com/cWbo3mPUh4Ho/?ref=app
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
+ 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?
+ 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