I was thinking along the lines of using typeid()
but I don't know how to ask if that type is a subclass of another class (which, by the way, is abstract)
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
dynamic_cast
can determine if the type contains the target type anywhere in the inheritance hierarchy (yes, it's a little-known feature that ifB
inherits fromA
andC
, it can turn anA*
directly into aC*
).typeid()
can determine the exact type of the object. However, these should both be used extremely sparingly. As has been mentioned already, you should always be avoiding dynamic type identification, because it indicates a design flaw. (also, if you know the object is for sure of the target type, you can do a downcast with astatic_cast
. Boost offers apolymorphic_downcast
that will do a downcast withdynamic_cast
andassert
in debug mode, and in release mode it will just use astatic_cast
).I don't know if I understand your problem correctly, so let me restate it in my own words...
Problem: Given classes
B
andD
, determine ifD
is a subclass ofB
(or vice-versa?)Solution: Use some template magic! Okay, seriously you need to take a look at LOKI, an excellent template meta-programming library produced by the fabled C++ author Andrei Alexandrescu.
More specifically, download LOKI and include header
TypeManip.h
from it in your source code then use theSuperSubclass
class template as follows:According to documentation,
SuperSubClass<B,D>::value
will be true ifB
is a public base ofD
, or ifB
andD
are aliases of the same type.i.e. either
D
is a subclass ofB
orD
is the same asB
.I hope this helps.
edit:
Please note the evaluation of
SuperSubClass<B,D>::value
happens at compile time unlike some methods which usedynamic_cast
, hence there is no penalty for using this system at runtime.You can only do it at compile time using templates, unless you use RTTI.
It lets you use the typeid function which will yield a pointer to a type_info structure which contains information about the type.
Read up on it at Wikipedia
Result:
The code below demonstrates 3 different ways of doing it:
The program above prints this:
I disagree that you should never want to check an object's type in C++. If you can avoid it, I agree that you should. Saying you should NEVER do this under any circumstance is going too far though. You can do this in a great many languages, and it can make your life a lot easier. Howard Pinsley, for instance, showed us how in his post on C#.
I do a lot of work with the Qt Framework. In general, I model what I do after the way they do things (at least when working in their framework). The QObject class is the base class of all Qt objects. That class has the functions isWidgetType() and isWindowType() as a quick subclass check. So why not be able to check your own derived classes, which is comparable in it's nature? Here is a QObject spin off of some of these other posts:
And then when you are passing around a pointer to a QObject, you can check if it points to your derived class by calling the static member function: