- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 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; }
Advertisements