C++: using typeinfo to test class inheritance

后端 未结 3 1835
一生所求
一生所求 2020-12-18 10:03

I have a pointer to a polymorphic type, p. I also have a type_info for a class somewhere in the same hierarchy, ti.

If I just

3条回答
  •  一生所求
    2020-12-18 10:22

    There's no way to do this in standard C++ alone. If you're using an implementation that has the Itanium C++ ABI1 you can do this though, for example:

    #include 
    #include 
    #include 
    #include 
    
    class base {
    protected:
      base() {
      }
    public:
      static bool check(const std::type_info& p) {
        // Find the real type_info for single inheritance non virtual 
        const __cxxabiv1::__si_class_type_info* test = dynamic_cast(&p);
        return test ? typeid(base) == *test->__base_type : false;
      }
    
      virtual ~base() {}
    };
    
    class der : public base {
    };
    
    class foo {};
    
    int main() {
      der d;
      foo f;
    
      std::cout << base::check(typeid(d)) << "\n";
      std::cout << base::check(typeid(f)) << "\n";
    }
    

    Here this works because the type has a single, non-virtuallly inherited base. You can support more cases though with care and similar dynamic_casts.

    Whilst this is possible under these certain circumstances I think you're solving the wrong problem though - a solution based around a std::map is more portable and avoids relying on implementation details like this.

    1 confusingly named, it's a surprisingly large compiler/architecture list, not just Itanium

提交回复
热议问题