Retrieving a c++ class name programmatically

前端 未结 5 1825
Happy的楠姐
Happy的楠姐 2020-12-04 13:23

I was wondering if it is possible in C++ to retrieve the name of a class in string form without having to hardcode it into a variable or a getter. I\'m aware that none of th

5条回答
  •  悲&欢浪女
    2020-12-04 13:34

    If you just want to check if it's certain class, then

    typeid(obj) == typeid(CSubClass)
    

    will always work regardless of the implementations.

    Otherwise, a convenient way is to declare:

    virtual const char* classname() { return "CMyClass";}
    

    and implement per subclass.

提交回复
热议问题