How to define a macro that retains its scope after being called
I am trying to make a macro to make it easier to define properties.
Simplified example, but for now I have this to provide a public and private set property:
#define propertyRO(xxType, xxName)\
property xxType xxName\
{\
xxType get() {return m___##xxName;}\
void set(xxType value) {m___##xxName = value;}\
}\
private:\
xxType m___##xxName;\
and then to use it, you would do the following:
public ref class Wawawa
{
public:
int bob;
propertyRO(String^, MyName);
};
This potentially works great, but it is wrong because the member is listed in the private scope, which means everything that happens after the macro gets the private scope as well. eg:
public ref class Wawawa
{
public:
int bob;
propertyRO(String^, MyName);
int fred; //ERROR HERE <- this would be private not public
};
So, if you ignore what this macro actually does, my real question is, is there a way to use a keyword private:
in a macro without affecting the rest of the class?
a source to share
I would answer: just don't do it. Don't use macros to generate code. This looks like it will save you time and effort, but you've already found that the macro has a problem that is of course not obvious to someone reading the class declaration. You may also soon find that debugging is a nightmare, as the debugger simply points to the macro, not the code inside.
IMO, bite the bullet and just write down all properties in full.
a source to share
I have a feeling that this will not be possible.
So, perhaps the best suggestion would be to rename the macro to publicPropertyRO
, which would make it obvious that it would create a public property. And move the private member to be above the property declaration:
#define publicPropertyRO(xxType, xxName)\
private:\
xxType m___##xxName;\
public:\
property xxType xxName\
{\
xxType get() {return m___##xxName;}\
void set(xxType value) {m___##xxName = value;}\
}\
That would leave the class in a state public:
, which I think is acceptable.
I haven't answered my question, but at least this is an improvement.
a source to share
The core of the problem is that the RO property macro changes the area under the hood, right? The problem is not that it changes the volume as such, but that it is done in a covert way, so the best way to deal with this is to expose it.
Demoncodemonkey 's suggestion is to embed public in the function name (which might be OK), but the alternative is to create a scope following the macro a parameter. For instance.
#define propertyRO(xxType, xxName, xxFollowingScope)\
property xxType xxName\
{\
xxType get() {return m___##xxName;}\
void set(xxType value) {m___##xxName = value;}\
}\
private:\
xxType m___##xxName;\
xxFollowingScope:
...
public ref class Wawawa
{
public:
int bob;
propertyRO(String^, MyName, public);
int fred; // No error here, fred is public
};
The syntax and naming is not very good, but it ensures that it is impossible to use this macro without consciously deciding what the scope after the function should be.
a source to share