There is "no problem" in calling an member function thru a null pointer, because the "member" function isn't an actual member of an object, the compiler just passes this pointer as a first parameter of the function.
You get into problems only when you try to access some members of that NULL this...
But if you do this "trick" it is a sign that some member function should be declared static or not member at all.
Example this code should work:
class A{
public:
A(){};
int sum(int x, int y) { return x + y;}
};
int main()
{
A* p = NULL;
std::cout << p->sum(1, 1); //here the compiler passes 'p' (the address of the object - as a 'this')
return 0;
};
class A{
public:
A(){};
int sum(int x, int y) { return x + y;}
};
int main()
{
A* p = NULL;
std::cout << p->sum(1, 1); //here the compiler passes 'p' (the address of the object - as a 'this')
return 0;
};
To copy to clipboard, switch view to plain text mode
And for your actual problem see what data your function access and how is your function called with null pointer the problem is most likely at that end.
Bookmarks