As null pointer points to nothing. So, How does the following code works
#include <stdio.h>
class cb
{
public:
void HelloB()
{
printf("Calling cb\n");
};
};

int main(void)
{
cb* pb = 0;
pb->HelloB(); // should it crash here ?
return 0;
}

As null pointer points to nothing. So, How does the following code works
#include <stdio.h>
class cb
{
public:
void HelloB()
{
printf("Calling cb\n");
};//This semicolon should not be here...
};

int main(void)
{
cb* pb = 0;
pb->HelloB(); // should it crash here ?
return 0;
}

Whereas crashing of program is concern(it won't..no reason)...it doesn't matter if u initailise it will 0.....member function will be called depending on the type of the pointer

You can even write

cb* pb=(cb*)10;

//although it doesn't make any sense..still the code wil will work

Sunny,
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. :)

but it is null ptr and null ptr cant be dereferenced .

Neither in your program nor the example which i gave you derefrenced null pointer.

Also, they cant be used for calling functions.

I guess here i don't need to prove anything as it does call the member function...try running it and check it out

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.

It is a pointer of type class cp..which can be passed null pointer or address of any other object.

Example

cb obj;
cb* pb = 0;
pb->somememberfunction();
pb=&obj;
pb->somememberfunction();

Both will work

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!

Neither in your program nor the example which i gave you derefrenced null pointer.

actually you do dereference the null pointer...

PT* ptr = 0;
ptr->whatever();

is the same as

PT* ptr = 0;

(*ptr).whatever();

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.