- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
C++ equivalent of instanceof
C++ has no direct method to check one object is an instance of some class type or not. In Java, we can get this kind of facility.
In C++11, we can find one item called is_base_of<Base, T>. This will check if the given class is a base of the given object or not. But, this does not verify whether the given class instance uses that function.
The nearest possible functionality similar to "instanceof" can be achieved using dynamic_cast <new-type>(expression). This tries to covert the given value into the specified type and returns the result. if the cast is unsuccessful this returns a null pointer. This works only for polymorphic pointers and when compiler RTTI is enabled.
Example Code
#include <iostream> using namespace std; template<typename Base, typename T> inline bool instanceof(const T *ptr) { return dynamic_cast<const Base*>(ptr) != nullptr; } class Parent { public: virtual ~Parent() {} virtual void foo () { std::cout << "Parent\n"; } }; class Child : public Parent { public: virtual void foo() { std::cout << "Child\n"; } }; class AnotherClass{}; int main() { Parent p; Child c; AnotherClass a; Parent *ptr1 = &p; Child *ptr2 = &c; AnotherClass *ptr3 = &a; if(instanceof<Parent>(ptr1)) { cout << "p is an instance of the class Parent" << endl; } else { cout << "p is not an instance of the class Parent" << endl; } if(instanceof<Parent>(ptr2)) { cout << "c is an instance of the class Parent" << endl; } else { cout << "c is not an instance of the class Parent" << endl; } if(instanceof<Child>(ptr2)) { cout << "c is an instance of the class Child" << endl; } else { cout << "c is not an instance of the class Child" << endl; } if(instanceof<Child>(ptr1)) { cout << "p is an instance of the class Child" << endl; } else { cout << "p is not an instance of the class Child" << endl; } if(instanceof<AnotherClass>(ptr2)) { cout << "c is an instance of AnotherClass class" << endl; } else { cout << "c is not an instance of AnotherClass class" << endl; } }
Output
p is an instance of the class Parent c is an instance of the class Parent c is an instance of the class Child p is not an instance of the class Child c is not an instance of AnotherClass class
- Related Articles
- Java instanceof operator
- instanceof Keyword in Java
- Instanceof operator in JavaScript
- instanceof operator in Java
- Java instanceof and its applications
- Equivalent of Java Super Keyword in C#
- What is the C++ equivalent of sprintf?
- What is the C# equivalent of C++ friend keyword?
- instanceof operator vs isInstance method in java
- What is instanceof operator in Java? Explain.
- How to check "instanceof" class in Kotlin?
- How to use the instanceof operator in Java?
- What is the C# Equivalent of SQL Server DataTypes?
- What is the equivalent of C# namespace in Java?
- C# Equivalent to Java Functional Interfaces

Advertisements