美文网首页
C++判断是否是子类

C++判断是否是子类

作者: 独步江雪 | 来源:发表于2021-05-18 01:16 被阅读0次
    #include <iostream>
    #define IS_INSTANCE_OF(INSTANCE,CLS) (typeid (INSTANCE) == typeid(CLS))
    class A{};
    class B{};
    
    int main()
    {
        A a;
        std::cout <<IS_INSTANCE_OF(a, A)<<std::endl;
        std::cout <<IS_INSTANCE_OF(a, B);
    }
    
    #include <iostream>
    #include<vector>
    #include<memory>
    
    #define IS_INSTANCE_OF(INSTANCE,CLS) (typeid (INSTANCE) == typeid(CLS))
    class A {};
    class B {};
    
    int main()
    {
        std::shared_ptr<A> a = std::make_shared<A>();
        std::cout << IS_INSTANCE_OF(*a ,B) << std::endl;
    }
    

    相关文章

      网友评论

          本文标题:C++判断是否是子类

          本文链接:https://www.haomeiwen.com/subject/fetejltx.html