
- 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 class method through NULL class pointer in C++
A class method can be can be called using a NULL class pointer.
Note − This is undefined behaviour and there is not 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 Example { public : void func() { cout << "The function is called through Null class pointer."; } }; int main() { Example *p = NULL; p->func(); return 0; }
Output
The output of the above program is as follows.
The function is called through Null class pointer.
Now, let us understand the above program.
The class Example contains a member function func(). This function displays "The function is called through Null class pointer." The code snippet for this is given as follows.
class Example { public : void func() { cout << "The function is called through Null class pointer."; } };
In the function main(), the class null pointer p is created. Then func() is called using p. The code snippet for this is given as follows.
int main() { Example *p = NULL; p->func(); return 0; }
- Related Articles
- Calling a member function on a NULL object pointer in C++
- Calling a method using null in Java\n
- Calling a JAVA method through JavaScript in SAPUI5 project
- NULL pointer in C
- Iterate through Triplet class in JavaTuples
- Iterate through Septet class in JavaTuples
- Iterate through Quartet class in JavaTuples
- Null Pointer Exception in C#
- Iterate through Quintet class in Java Tuples
- Differentiate the NULL pointer with Void pointer in C language
- Class method vs static method in Python
- Null Pointer Exception in Java Programming
- NavigableSet Class floor() method in Java
- NavigableSet Class higher() method in Java
- NavigableSet Class lower() method in Java

Advertisements