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

前端 未结 3 1824
再見小時候
再見小時候 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 21:12

    If you are following strictly standard C++, you may need to just bite the bullet and do some additional bookkeeping. Make an enum somewhere that stores all the classnames:

    enum ClassID {
        MYTASK1_CLASS,
        MYTASK2_CLASS
    };
    

    It doesn't take that long to add a new classId when you make a new class.

    I've done this before. It's sufficient for uniqueness to do what I describe above. But... if the enum values are set with a clever enough macro, you can encode the hierarchy of the classes, and implement dynamic cast and instanceof solely from the ClassID and a bitwise mask!

提交回复
热议问题