but it is null ptr and null ptr cant be dereferenced . Also, they cant be used for calling functions. A null pointer is known not to point to any object or function.
It is not the initialization, it is saying that ptr is null.
Now look into the problem again. :)
it will compile, but dereferencing a null pointer like that will often (but maybe not always) crash the program -- it's undefined behavior. In the case of c++ classes, the function will be called with an invalid "this" pointer and any attempt to reference or use class objects will also result in undefined behavior. So "it works" is all relative to this undefined behavior, and won't "work" very long!
Sorry about that...yes it does derefrence the null pointer.....as far as working of code is concerned...what you are doing is wrong
Almost always when a program does something wrong like this, the C++ standard does NOT says that the program must crash, its does NOT say that the program must produce an error message, what it says is that the program has UNDEFINED BEHAVIOUR.
UNDEFINED BEHAIOUR means exactly what it says, anything could happen,
including the program working. If you ran this program on a different
computer, or with a different compiler or even on a different day of the
week you might get different behaviour.
Most compilers will not crash untill you access a member variable
within the function.
In other Words,
Since the member function in this case does not use its "this"
pointer for anything.....therefore it will work most of the time