Using macros to declare classes?!

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
The #define macro is basically a precompiler time find and replace operation. There's not too much about it that cool.
How would you handle object creation? Or constructors?
Last edited on
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
{
//...
};

@Computergeek01
I know the #define is for setting up replacement macros but this is what I want it for.
So basically I can create a class and as the program reads the macro it should be replaced by the compiler right??

So if I had:
1
2
3
4
5
#define CLASSNAME "Object"

class CLASSNAME{
  //class definition
}

The CLASSNAME macro should be completely replaced with "Object" shouldn't it? Thus giving the class definition the same functionality as:
1
2
3
class Object{
  //class definition
}


I think this would work somehow but I'm not sure if it'd need the 'stringify #' or not? Or if it'd work at all?



@ResidentBiscuit
That's the beauty, I'm not wanting to use constructors and destructors for these classes. I plan to create derived classes which are basically just containers of a superclass with only values changed... Trust me I know a way how to make it work if I can do this!
Last edited on
The CLASSNAME macro should be completely replaced with "Object" shouldn't it?


Yes but it will replace it literally with the string "Object" not with the name Object.

IE this:
1
2
3
4
5
#define CLASSNAME "Object"

class CLASSNAME{
  //class definition
};


becomes this:
1
2
3
4
5
#define CLASSNAME "Object"

class "Object"{  // <- compiler error here
  //class definition
};



You probably meant to do this:

 
#define CLASSNAME Object 


But again I really don't see the point to this because you can just use Object directly and not bother with the macro at all.



EDIT:

Also note you can't use the # stringize token outside of macros. They can only be used inside the #define itself. So this:

cout << "Macro MAC contains: " << #MAC << endl;

Would give you an error because # is being used outside of a macro definition.
Last edited on
There's a great way to answer these kind of things; test it out. What's the point of this, though?
What's the point of this, though?


I've been wondering this as well. Still not sure what the OP is actually trying to do other than obfuscate his code with unnecessary macros.
@ResidentBiscuit
Well yes I was planning to test it out but I was away from my computer when I wanted to know so I thought I'd ask if it was possible and if anybody had any tips or problems I may need to face first.

@Disch
Ah thanks for that, and trust me I have my uses! XD
Basically I want a program that is practically nothing and in a way, practically compiles itself based on files in a specified directory.

You might see if my project is one day complete.
Don't all programs compile themselves in that sense?

Still confused... but whatever. As long as you're enjoying what you're doing I guess it's fine. Don't let old farts like me dissuade you. :)
Topic archived. No new replies allowed.