template<class A, class X>class T...
class B: public A{...void methodB();};class C: public A{...void methodC1();virtual void methodC2();};class D: public C{...virtual void methodC2();};
void T<A,X>::doStuff(){// if A is B:this->methodB();return;// else if A is D:this->methodC2(); // тут надо вызвать метод из класса D...return;// else if A is C:this->methodC2(); // тут надо вызвать метод из класса C...}
template<class X>void T<D,X>::doStuff(){this->methodC2();}
C++ (Qt)struct A{ void print() { cout << "A" << endl; }}; struct B : public A{ void printB() { cout << "B" << endl; }}; struct C : public A{ void printC() { cout << "C" << endl; }}; template <class>struct helper; template<>struct helper<A>{ static void method(A & obj) { obj.print(); }}; template<>struct helper<B>{ static void method(B & obj) { obj.printB(); }}; template<>struct helper<C>{ static void method(C & obj) { obj.printC(); }}; template <class _A>class T{public: void doStuff() { helper<_A>::method(m_obj); } private: _A m_obj;}; int main(){ T<A> objA; T<B> objB; T<C> objC; objA.doStuff(); objB.doStuff(); objC.doStuff(); return 0;}
// if this is A:if (this->methodA() == BlaBla1){ ... handle BlaBla1 case m_flags = SomeFlags1; ... }else{ ... handle BlaBla2 case if (m_flags & SomeOtherFlags) doAnotherStuff(); else m_flags = SomeFlags2; ... }// if this is C...
void T<A>::doStuff(){switch (type of A){case A: methodA();...case D: methodC(); methodD();...}}
C++ (Qt)#include <iostream> using namespace std; struct A{ void print() { cout << "A" << endl; }}; struct B : public A{ void printB() { cout << "B" << endl; }}; struct C : public A{ void printC() { cout << "C" << endl; }}; template <class>struct helper; template<>struct helper<A>{ static void method(A & obj) { obj.print(); }}; template<>struct helper<B>{ static void method(B & obj) { obj.printB(); }}; template<>struct helper<C>{ static void method(C & obj) { obj.printC(); }}; template <bool>struct case_of_helper; template <>struct case_of_helper<true>{ template <class F> static void doStuff(F f) { f(); }}; template <>struct case_of_helper<false>{ template <class F> static void doStuff(F) {}}; template <class Base, class Derived, class F>void meta_case_of(F f){ case_of_helper<std::is_same<Base, Derived>::value>::doStuff(f);} template <class Obj>class T{public: void doStuff() { meta_case_of<A, Obj>([&]() { helper<Obj>::method(m_obj); }); meta_case_of<B, Obj>([&]() { helper<Obj>::method(m_obj); }); meta_case_of<C, Obj>([&]() { helper<Obj>::method(m_obj); }); } private: Obj m_obj;}; int main(){ T<A> objA; T<B> objB; T<C> objC; objA.doStuff(); objB.doStuff(); objC.doStuff(); return 0;}
if (typeid(some_member) == typeid(some_class))