Using macros to declare classes?!

SatsumaBenji (133)
Ok this is a really interesting question that could be rather useful in the right situations but I'm not sure if it's possible.
I've been doing a little reading on macros for something else when I came across "stringification". The tutorial says that if I define a macro to a certain value or string I can convert this macro to what it defines by using the '#'.

i.e.
1
2
#define MAC "macroString"
cout << "Macro MAC contains: " << #MAC << endl; 

macroString


So... Is it possible to create classes using a the definition of the macro to setup the class name?
i.e.
1
2
3
4
#define MAC "Object"
class #MAC{
  //class definition
}


Would this work (if it works at all) with the same functionality as:
1
2
3
class Object{
  //class definition
}
Last edited on
Computergeek01 (2873)
The #define macro is basically a precompiler time find and replace operation. There's not too much about it that cool.
ResidentBiscuit (2205)
How would you handle object creation? Or constructors?
Last edited on
Disch (8336)
You mean for some kind of reflection?

There wouldn't be much point to it. Closest you can do is this:

1
2
3
4
5
6
7
#define CLASSNAME MyClass
#define CLASSNAMESTRING #CLASSNAME // not sure if this would even work

class CLASSNAME
{
//...
};


But as you can see that's pointless, because it has the same effects as this:

1
2
3
4
5
const char* ClassNameString = "MyClass";
class MyClass
{
//...
};

Registered users can post here. Sign in or register to post.