Let's say I have class SuperClass { public: int a; }
and class SubClass : SuperClass { public: int b; }
and I took a pointer to an instance of the SubClass SubClass *subPointer
and addressed that pointer to a SuperClass pointer SuperClass *superPointer = subPointer
. Now of course I can always cast the superPointer
object to a pointer of SubClass because the only thing it stores is an adress. But how would I know if the object superPointer
is pointing to an instance of SubClass or is just a SuperClass pointer?

- 115
- 3
- 8
-
This can't be done in general, though RTTI will tell you in most cases if you compile with it. – Antimony Jul 12 '13 at 02:30
-
In addition to @Antimony's suggestions there are ways to build this knowledge into the objects themselves, but they generally require that everything inherits from some god class (yet another `TObject`, anyone?). Most people will try to dissuade you from doing this. – dmckee --- ex-moderator kitten Jul 12 '13 at 02:34
-
possible duplicate of [Finding the type of an object in C++](http://stackoverflow.com/questions/351845/finding-the-type-of-an-object-in-c) – jogojapan Jul 12 '13 at 02:36
-
As the proposed duplicate indicates, the easiest thing to do is to add a virtual destructor to `SuperClass`, and then test the result of `dynamic_cast
(superPointer)`. – jxh Jul 12 '13 at 03:06 -
See it in action here: http://ideone.com/hUiIfE – paddy Jul 12 '13 at 03:10
2 Answers
You usually don't want to use typeid
for this.
You usually want to use dynamic_cast
instead:
if (SubClass *p = dynamic_cast<SubClass *>(SuperClassPtr))
// If we get here (the `if` succeeds) it was pointing to an object of
// the derived class and `p` is now pointing at that derived object.
A couple of notes though. First of all, you need at least one virtual function in the base class for this to work (but if it doesn't have a virtual function, why are you inheriting from it?)
Second, wanting this very often tends to indicate design problems with the code. In most cases, you want to define a virtual function in the base class, which you (if necessary) override in the derived class to do whatever's needed so you can just use a pointer to the base class throughout.
Finally, as it stands right now, most of the conversions will fail -- you've used the default (private) inheritance, which prevents the implicit conversion from derived *
to base *
that you'd normally expect to see happen (you probably want class SubClass : public SuperClass
).

- 476,176
- 80
- 629
- 1,111
-
1Easy to have at least one virtual function - just make a virtual destructor. You need that anyway. – paddy Jul 12 '13 at 03:08
-
@paddy, Too bad there's no easy way to do `virtual ~Class() = 0 = default;` – chris Jul 12 '13 at 03:18
-
+1 For pointing out that the need for `dynamic_cast` usually indicates a design-flaw. – ComicSansMS Jul 12 '13 at 07:08
Use RTTI machanism. Like:
if(typeid(*superPointer) == typeid(SuperClass)) superPointer->dosomething();
if(typeid(*superPointer) == typeid(SubClass)) superPointer->dosomethingelse();

- 6,707
- 7
- 32
- 49
-
3Have you tested this? I have come across issues where this returns the superPointer's information every time. – Sellorio Jul 12 '13 at 02:40