Is the type check a mere integer comparison? Or would it make sense to have a GetTypeId virtual function to distinguishing which would make it an integer comparison?
(Just don’t want things to be a string comparison on the class names)
EDIT: What I mean is, if I’m often expecting the wrong type, would it make sense to use something like:
struct Token
{
enum {
AND,
OR,
IF
};
virtual std::size_t GetTokenId() = 0;
};
struct AndToken : public Token
{
std::size_t GetTokenId() { return AND; }
};
And use the GetTokenId member instead of relying on dynamic_cast.
The functionality of the
dynamic_castgoes far beyond a simple type check. If it was just a type check, it would be very easy to implement (something like what you have in your original post).In addition to type checking,
dynamic_castcan perform casts tovoid *and hierarchical cross-casts. These kinds of casts conceptually require some ability to traverse class hierarchy in both directions (up and down). The data structures needed to support such casts are more complicated than a mere scalar type id. The information thedynamic_castis using is a part of RTTI.Trying to describe it here would be counterproductive. I used to have a good link that described one possible implementation of RTTI… will try to find it.