Why am I able to make a function call using an invalid class pointer

The C++ compiler doesn’t prevent you from using uninitialised pointers, and although the results are undefined, it’s normal for compilers in the real world to generate code that ignores the fact that the pointer is uninitialised.

It’s one of the reasons why C++ is both fast and (comparatively) dangerous relative to some other languages.

The reason your call to func2 succeeds is that it doesn’t touch its this pointer. The pointer value is never used, so it can’t cause a problem. In func1 you do use the this pointer (to access a member variable), which is why that one crashes.

Leave a Comment