tags:

views:

9492

answers:

8

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?

+5  A: 

This is called RTTI, but you almost surely want to reconsider your design here, because finding the type and doing special things based on it makes your code more brittle.

Paul Betts
True. Unfortunately I'm working on an existing project so I can't really go changing the design, or anything in class A.
lemnisca
+1  A: 

Are you looking for dynamic_cast<B*>(pointer)?

Joshua
+2  A: 

Your description is a little confusing.

Generally speaking, though some C++ implementations have mechanisms for it, you're not supposed to ask about the type. Instead, you are supposed to do a dynamic_cast on the pointer to A. What this will do is that at runtime, the actual contents of the pointer to A will be checked. If you have a B, you'll get your pointer to B. Otherwise, you'll get an exception or null.

Uri
fpsgamer
Yea, I should have clarified that better. Thanks. I wish there was a word the describe in C types that are by-reference rather than by-value.
Uri
A: 

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.

freespace
+13  A: 

dynamic_cast should do the trick

TYPE& dynamic_cast<TYPE&> (object);
TYPE* dynamic_cast<TYPE*> (object);

The dynamic_cast keyword casts a datum from one pointer or reference type to another, performing a runtime check to ensure the validity of the cast.

If you attempt to cast to pointer to a type that is not a type of actual object, the result of the cast will be NULL. If you attempt to cast to reference to a type that is not a type of actual object, the cast will throw a bad_cast exception.

Make sure there is at least one virtual function in Base class to make dynamic_cast work.

yesraaj
+1 also for the last comment. That's an important requirement to remember.
Stefano Borini
+1  A: 

As others indicated you can use dynamic_cast. But generally using dynamic_cast for finding out the type of the derived class you are working upon indicates the bad design. If you are overriding a function that takes pointer of A as the parameter then it should be able to work with the methods/data of class A itself and should not depend on the the data of class B. In your case instead of overriding if you are sure that the method you are writing will work with only class B, then you should write a new method in class B.

Naveen
A: 

Use overloaded functions. Does not require dynamic_cast or even RTTI support:

class A {};
class B : public A {};

class Foo {
public:
    void Bar(A& a) {
        // do something
    }
    void Bar(B& b) {
        Bar(static_cast<A&>(b));
        // do B specific stuff
    }
};
jmucchiello
Right from original question: "I later call functions that only B has" - how overloading would work in such case?
Marcin Gil
When you call Bar with an A, no B stuff happens. When you call Bar with a B, methods that only exist on B can be called. Do you read the original question? Bar is his "I am overriding a function that accepts an object of type A as a parameter"
jmucchiello
This doesn't work with dynamic polymorphism, which I suspect the questioner is using. C++ can't select an overload based on the runtime class of the parameter, only based on the compile-time type.
Steve Jessop
A: 

When exactly did it become bad design to implement abstract classes (e.g. as an interfac) and checking if an object implements it?

ElConso
Generally a concrete implementation should fully implement its parent's abstract interface, not pick and choose parts of it (else you lose substitutibility).
Mark B