Calling a member function on a NULL object pointer in C++



You can call a class member function using a NULL object pointer in C++. This is an undefined behavior and there is no guarantee about the execution of the program. The actual results depend on the compiler used.

Class Member Function

A class member function is a function that is defined either inside a class or outside a class using a scope resolution operator.

Example

Here is an example of class member functions in C++ where the print() function is an example of the Inside class function and the print2() function is an example of an Outside class function:

#include <iostream>
using namespace std;

class Method
{
public:
   void print()
   {
      cout << "This is a member function inside the class." << endl;
   }

   void print2();
};

void Method::print2()
{
   cout << "This is a member function outside the class." << endl;
}

int main()
{
   Method obj;
   obj.print();
   obj.print2();
   return 0;
}

The output of the above code is as follows:

This is a member function inside the class.
This is a member function outside the class.

Null Pointer

A pointer that is assigned NULL is called a null pointer and is used to check if the pointer is pointing to any valid address or not. It is declared using nullptr, NULL, or by assigning with 0.

Example

Here is an example of a null pointer where the if/else condition returns whether the pointer ptr is null or not.

#include <iostream>
using namespace std;
int main()
{
   int *ptr = NULL;
   if (ptr == NULL)
   {
      cout << "Pointer is NULL" << endl;
   }
   else
   {
      cout << "Pointer is not NULL" << endl;
   }
   return 0;
}

The output of the above code is as follows:

Pointer is NULL

Calling Member Function Through Null Object Pointer

In this example, we have called a function func() using a null object pointer *p. This code runs without throwing any error and showing any undefined behavior.

#include <iostream>
using namespace std;
class Example
{
public:
   void func()
   {
      cout << "The function is called through Null object pointer.";
   }
};
int main()
{
   Example *p = NULL;
   p->func();
   return 0;
}

The output of the above code is as follows:

The function is called through Null object pointer.

Calling Virtual Function Through Null Object Pointer

A virtual member function is declared in the base class using the virtual keyword. If we use a null object pointer to call a virtual member function, it will throw an error.

Example

Here is an example of calling a virtual member function print() through null pointer *ptr that results in the segmentation fault:

#include <iostream>
using namespace std;

class Example
{
public:
   virtual void print()
   {
      cout << "A virtual member function called through null object pointer" 
           << endl;
   }
};

int main()
{
   Example *ptr = nullptr;

   ptr->print();

   return 0;
}

The output of the above code is as follows:

Segmentation fault (core dumped)

Calling Static Function Through Null Object Pointer

A static member function is defined using the static keyword and can be directly called using just the class name, without creating an object. It does not give any error and runs without any error as it does not require creating any object.

Example

The following example calls the static function print() using a null pointer *ptr to print the msg in the print() function.

#include <iostream>
using namespace std;

class Example
{
public:
   static void print()
   {
      cout << "A static member function called through null object pointer" 
           << endl;
   }
};

int main()
{
   Example *ptr = nullptr; 
   ptr->print();
   return 0;
}

The output of the above code is as follows:

A static member function called through null object pointer
Updated on: 2025-06-13T18:53:46+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements