Creating a new object from dynamic type info

Clone method There is nothing provided by the language that queries type and lets you construct from that information, but you can provide the capability for your class hierarchy in various ways, the easiest of which is to use a virtual method: struct Base { virtual ~Base(); virtual std::auto_ptr<Base> clone(/*desired parameters, if any*/) const = … Read more

What can make C++ RTTI undesirable to use?

There are several reasons why LLVM rolls its own RTTI system. This system is simple and powerful, and described in a section of the LLVM Programmer’s Manual. As another poster has pointed out, the Coding Standards raises two major problems with C++ RTTI: 1) the space cost and 2) the poor performance of using it. … Read more

Which is faster between is and typeof

Does it matter which is faster, if they don’t do the same thing? Comparing the performance of statements with different meaning seems like a bad idea. is tells you if the object implements ClassA anywhere in its type heirarchy. GetType() tells you about the most-derived type. Not the same thing.

Accessing protected event of TWinControl

You don’t need RTTI. Any code has implicit access to the protected members of any class declared in the same unit. You can take advantage of this by declaring a new TWinControl descendant in the unit that needs access to that class’s members. The declaration is very simple: type TProtectedWinControl = class(TWinControl); Then type-cast any … Read more

Why do I get “type has no typeinfo” error with an enum type

Discontiguous enumerations, and enumerations which don’t start at zero, don’t have typeinfo. For typeinfo to be implemented, it would need to be in a different format from the existing tkEnumeration, owing to backward compatibility issues. I considered implementing a tkDiscontiguousEnumeration (or possibly better named member) for Delphi 2010, but the benefit seemed small considering their … Read more

How expensive is RTTI?

Regardless of compiler, you can always save on runtime if you can afford to do if (typeid(a) == typeid(b)) { B* ba = static_cast<B*>(&a); etc; } instead of B* ba = dynamic_cast<B*>(&a); if (ba) { etc; } The former involves only one comparison of std::type_info; the latter necessarily involves traversing an inheritance tree plus comparisons. … Read more