this == null; surely not, but yes

Posted by pm100 on Stack Overflow See other posts from Stack Overflow or by pm100
Published on 2010-05-11T16:36:54Z Indexed on 2010/05/11 16:44 UTC
Read the original article Hit count: 184

Filed under:

I would have said that this will segfault but apparently not

#include <stdio.h>
class foo
{
public:
   bool thingy() {return this == 0;};
};


int main()
{
  foo *f = 0;
  if(f->thingy())
    printf("eating hat\n");
}

I just expect that 0->xxx will never work.

I assume that if we had to do some vtabling it would fail; obviously cannot access any member variables either

edit; sorry - no question there. THe question is "are you surprised like me?"

and it seems the answer is yes. even though nobody said yes. I got

a) of course this will work b) of course this will never work c) it is undefined behavior; your mileage will vary

© Stack Overflow or respective owner

Related posts about c++