
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Function pointer to member function in C++
- Calling class method through NULL class pointer in C++
- NULL pointer in C
- Calling a method using null in Java
- Null Pointer Exception in C#
- Calling a Function in Python
- Differentiate the NULL pointer with Void pointer in C language
- Function Pointer in C
- What should we assign to a C++ pointer: A Null or 0?
- How to declare a pointer to a function in C?
- Why do we check for a NULL pointer before deleting in C/C++?
- Get the Name of a Member Object in Java
- Program with calling function in c language
- Null Pointer Exception in Java Programming
- A C/C++ Pointer Puzzle?
Advertisements