Automatic Class Names

Started by
1 comment, last by gimp 19 years, 11 months ago
Currently I have a CObject base class. It encomapsses: -The ability to become a heirachy(parent\child pointers\cleanup) -The classes name. Currently the name is just set by a user class like this: CObject::SetName("Model"); ...in the users constructor. That kinda sucks doesn''t it. I should have an object register that just links the object against a string name on creation. Besides that every time I create a new class I have to remember to change the user class name. I''d love to use __CLASS__ but that doesn''t exist... Any idea''s?
Chris Brodie
Advertisement
well if descending into the hell of preprocessor macros doesn''t bother you, you could define a macro like this...

#define DEF_CLASS(className) \class className \{ \    virtual std::string getClassName() { return #className; };\ 


then call define getClassName as a pure virtual in your base class and call it from the base constructor.

That solves your initial problem, but it makes your code pretty ugly and brings all the evil problems the macros always do.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
If you have RTTI enabled for your project, you can probably just use typeid() on your classes and get at the name via std::type_info::name().

This topic is closed to new replies.

Advertisement