C++ unique static id and class name with base class

前端 未结 3 1825
再見小時候
再見小時候 2021-01-22 20:32

Having class TaskBase, each derived class of it must have name and unique id.

The TaskBase is something like below:

class TaskB         


        
3条回答
  •  借酒劲吻你
    2021-01-22 20:57

    I suggest implementing pure virtual methods for obtaining the class name and ID in the base class. The descendants would need to provide the unique names and IDs.

    class TaskBase
    {
        public:
            virtual std::string  get_task_name(void) const = 0;
            virtual unsigned long get_task_id(void) const = 0;
    };
    

    I took @VoidStar's suggest a step further and put the names into a (single) common class:

    class TaskNames
    {
      protected:
        static std::string get_tas1_name();
    };
    
    class Task1: public TaskBase, public TaskNames
    {
    //...
    };
    

提交回复
热议问题