The previous problem description was ambiguous, so I modified something below. Thanks.
I want to implement some macros like this:
#define AddVariable(x) \
#define x (++counter)
class Base {
private:
int counter = 0;
}
class Extend : public Base {
public:
void Handler() {
AddVariable(ASDF);
AddVariable(HJKL);
// Here may add more variables.
}
void AnotherHandler() {
// It calls ASDF, HJKL too.
}
}
The ASDF and HJKL should be available through all handlers in the cpp file, so I have to define it in the macro (it's not a good design, though). But how should I write the proper macros to achieve it (#define cannot be nested in another #define)? Or is there another better way of implementation?
Thanks in advance.
Update:
A potential implementation is
#define AddVariable(x) \
int x = ++counter;
It works, but x is not global, and I have to fix this.