Help me find a quick method for checking the type of an object. There was a simple public inheritance and I used dynamic_cast . It was all good.
class BaseVirtual{}; class A:public BaseVirtual {}; class B:public BaseVirtual {}; bool isA(BaseVirtual * x){ return dynamic_cast<A*>(x);} Then a class with protected inheritance appeared, but dynamic_cast refuses to work, it always returns nullptr .
class C:protected BaseVirtual {}; bool isC(BaseVirtual * x){ return dynamic_cast<C*>(x);} // всегда FALSE Once used virtual functions, but refused for speed. Switched to dynamic_cast .
//--- результат опроса --- Suppose I returned to the implementation using virtual functions. Imagine that all classes are inherited in protected mode. How many methods you need to determine if the classes for example fifty Therefore, the question remains the same: how to get rid of virtual methods?
// g++ -std=c++11 protclas.cpp -o protclas # include <iostream> class A{ public: virtual bool isB(){return false;} virtual bool isC(){return false;} virtual ~A(){} }; class B:public A{ public: virtual ~B(){} virtual bool isB(){return true;} }; class C:protected A{ public: virtual ~C(){} virtual bool isC(){return true;} }; int main(){ A * x=reinterpret_cast<A*>( new C); A * y=reinterpret_cast<A*>( new B); std::cout<<"x=C:(x->isC())="<<(x->isC())<<std::endl; std::cout<<"y=B:(y->isC())="<<(y->isC())<<std::endl; delete y; delete x;}
isCclass method? - Andrej Levkovitchdynamic_castworks faster? - ixSci