How to get class name?

前端 未结 6 1361
萌比男神i
萌比男神i 2020-12-15 08:51

If I defined a class:

class Blah {};

How can I:

std::string const className = /* What do I need to do here? */;
assert( cla         


        
6条回答
  •  一个人的身影
    2020-12-15 09:53

    Like this:

    class Blah { static std::string const className() { return "Blah"; }};
    
    std::string const name = Blah::className();
    assert(name == "Blah");
    

    Or this:

    class Blah {};
    
    template < typename T > struct name;
    template < > struct name { static std::string value() { return "Blah"; }};
    
    std::string const classname = name::value();
    assert(classname == "Blah");
    

    Fancier:

    #define DECLARE_NAMED_CLASS( Name ) \
    struct Name;\
    template < > struct name { static std::string value() { return #Name; }};\
    struct Name
    
    DECLARE_NAMED_CLASS(Blah) {};
    std::string const className = name::value();
    ...
    

    Or this:

    class Blah : QObject { Q_OBJECT };
    

    Or this:... Or this: ...

提交回复
热议问题