
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Calling a member function on a NULL object pointer in C++
A class member function can be called using a NULL object pointer.
Note − This is undefined behaviour and there is no guarantee about the execution of the program. The actual results depend on the compiler used.
A program that demonstrates this is given as follows.
Example
#include <iostream> using namespace std; class Demo { public : void fun() { cout << "This member function is called through Null object pointer."; } }; int main() { Demo *ptr = NULL; ptr->fun(); return 0; }
Output
The output of the above program is as follows.
This member function is called through Null object pointer.
Now, let us understand the above program.
The class Demo contains a member function fun(). This function displays "This member function is called through Null object pointer." The code snippet for this is given as follows.
class Demo { public : void fun() { cout << "This member function is called through Null object pointer."; } };
In the function main(), the object null pointer ptr is created. Then the member function fun() is called using ptr. The code snippet for this is given as follows.
int main() { Demo *ptr = NULL; ptr->fun(); return 0; }
- Related Articles
- Function pointer to member function in C++
- Calling class method through NULL class pointer in C++
- NULL pointer in C
- Null Pointer Exception in C#
- Calling a method using null in Java\n
- Calling a Function in Python
- Differentiate the NULL pointer with Void pointer in C language
- Function Pointer in C
- Why do we check for a NULL pointer before deleting in C/C++?
- What should we assign to a C++ pointer: A Null or 0?
- How to declare a pointer to a function in C?
- Null Pointer Exception in Java Programming
- Declare a C/C++ function returning pointer to array of integer function pointers
- Why is address zero used for the null pointer in C/C++?
- How to declare member function in C# interface?

Advertisements