Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent of java's instanceof

What is the preferred method to achieve the C++ equivalent of java's instanceof?

like image 576
Yuval Adam Avatar asked Feb 01 '09 09:02

Yuval Adam


People also ask

Does C++ have 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>.

What is the difference between Instanceof and isInstance?

The instanceof operator and isInstance() method both are used for checking the class of the object. But the main difference comes when we want to check the class of objects dynamically then isInstance() method will work. There is no way we can do this by instanceof operator.

Is instance of a code smell?

Probably most of you have already heard that using “instanceof” is a code smell and it is considered as a bad practice. While there is nothing wrong in it and may be required at certain times, but the good design would avoid having to use this keyword.

What is an instance C++?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.


1 Answers

Try using:

if(NewType* v = dynamic_cast<NewType*>(old)) {    // old was safely casted to NewType    v->doSomething(); } 

This requires your compiler to have rtti support enabled.

EDIT: I've had some good comments on this answer!

Every time you need to use a dynamic_cast (or instanceof) you'd better ask yourself whether it's a necessary thing. It's generally a sign of poor design.

Typical workarounds is putting the special behaviour for the class you are checking for into a virtual function on the base class or perhaps introducing something like a visitor where you can introduce specific behaviour for subclasses without changing the interface (except for adding the visitor acceptance interface of course).

As pointed out dynamic_cast doesn't come for free. A simple and consistently performing hack that handles most (but not all cases) is basically adding an enum representing all the possible types your class can have and check whether you got the right one.

if(old->getType() == BOX) {    Box* box = static_cast<Box*>(old);    // Do something box specific } 

This is not good oo design, but it can be a workaround and its cost is more or less only a virtual function call. It also works regardless of RTTI is enabled or not.

Note that this approach doesn't support multiple levels of inheritance so if you're not careful you might end with code looking like this:

// Here we have a SpecialBox class that inherits Box, since it has its own type // we must check for both BOX or SPECIAL_BOX if(old->getType() == BOX || old->getType() == SPECIAL_BOX) {    Box* box = static_cast<Box*>(old);    // Do something box specific } 
like image 51
Laserallan Avatar answered Sep 18 '22 15:09

Laserallan