I know I will be punished after asking this, still I’d like to do something like this:
#define DEF_CLASS(x) \
#define CLASS x \
#define CONSTRUCTOR CLASS::CLASS \
#define COPY_CONSTRUCTOR(x) CONSTRUCTOR(const CLASS& x)
That is I’d like to have a “#define” function which dynamically defines other “#defines”.
I have an IDE which is not capable of refactoring, furthermore I’d like to make constructors, especially copy constructor to stand out due to its special role.
Therefore I aim to program my c++ class definitions like this:
CONSTRUCTOR(int i):i(i){}
COPY_CONSTRUCTOR(other):i(other.i){}
void CLASS::fun1()
{
//...
}
For that, currently I need to copy&paste three #define lines. I would rather do those automatically. Is there any way to pull this off using the preprocessor?
No, such a
DEF_CLASS(x)macro is not possible. Macros aren’t stateful. The best you can do is:But I strongly urge you not to abuse the preprocessor like this. Why obfuscate the normal C++ syntax with these macros? Accept C++ for what it is; don’t try to make it into a different, friendlier language.