C++判断是否是子类

2021-05-18  本文已影响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;
}
上一篇下一篇

猜你喜欢

热点阅读