How to get class name?

前端 未结 6 1354
萌比男神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:39

    I don't think there's any non-compiler specific solution to such problem that does not involve plenty of macros in the class declaration (actually if I understood correctly the QT documentation the string you get with objectName is actually assigned "by hand", I think with code created by moc).

    On the other hand, in general to check if the class of an object is one you don't want you shouldn't do a string comparison, but instead make a typeid comparison.

    assert(typeid(YourObject)==typeid(Blah));
    

    But probably you should explain better what you're trying to achieve.

提交回复
热议问题