Downcasting from base pointer to templated derived types

前端 未结 4 2011
夕颜
夕颜 2021-02-01 11:06

I have the following hierarchy:

class base
{
public:
   virtual ~base(){}
   virtual void foo() {}
};

template 
class derived1 : public base
{         


        
4条回答
  •  臣服心动
    2021-02-01 11:41

    You could add a virtual method to do a meta-type check of some kind:

    class base
    {
    public:
        ~base(){}
        virtual void foo() {}
        virtual bool isa(const char* type_to_test){
              return strcmp(type_to_test,"base")==0;}
    };
    
    template 
    class derived1 : public base
    {
       virtual void foo() {};
       virtual bool isa(const char* type_to_test){
       return strcmp(type_to_test,"derived1")==0;}
    };
    

提交回复
热议问题