I have a class A and another class that inherits from it, B. I am overriding a function that accepts an object of type A as a parameter, so I have to accept an A. However, I later call functions that only B has, so I want to return false and not proceed if the object passed is not of type B.
What is the best way to find out which type the object passed to my function is?
Just to be complete, I'll build build off of Robocide and point out that
typeid
can be used alone without using name():Output:
Because your class is not polymorphic. Try:
Now
BaseClas
is polymorphic. I changed class to struct because the members of a struct are public by default.Probably embed into your objects an ID "tag" and use it to distinguish between objects of class A and objects of class B.
This however shows a flaw in the design. Ideally those methods in B which A doesn't have, should be part of A but left empty, and B overwrites them. This does away with the class-specific code and is more in the spirit of OOP.
Use overloaded functions. Does not require dynamic_cast or even RTTI support:
Are you looking for
dynamic_cast<B*>(pointer)
?