我熟悉C++ RTTI,发现这个概念很有意思.
仍然存在许多滥用它的方法而不是正确使用它(RTTI-switch恐惧会浮现在脑海中).作为开发人员,我发现(并且使用过)只有两种可行的用途(更确切地说,一个半).
您能否分享一些RTTI是问题的可行解决方案,包括示例代码/伪代码?
注意:目标是拥有一个可供初级开发人员可以咨询,批评和学习的可行示例的存储库.
编辑:您将使用C++ RTTI找到下面的代码
// A has a virtual destructor (i.e. is polymorphic)
// B has a virtual destructor (i.e. is polymorphic)
// B does (or does not ... pick your poison) inherits from A
void doSomething(A * a)
{
// typeid()::name() returns the "name" of the object (not portable)
std::cout << "a is [" << typeid(*a).name() << "]"<< std::endl ;
// the dynamic_cast of a pointer to another will return NULL is
// the conversion …Run Code Online (Sandbox Code Playgroud)