Reputation: 8980
In C++, I am working with a macro where I need to define two variables, the first of which should have the same public / private / protected status as the scope where it is defined, while the other should plainly be private. However, if I just do:
#define my_macro(a, b) int a;\
private: int b;
then I have permanently changed the scope! This is not good. So I am wondering: is there any way to say: "only this variable / member / method / class is private"?
I naively tried doing something like
class my_class
{
public:
int i;
private int j;
};
But no luck. Is there any way to get what I need?
EDIT: I am not sure that the scope in which the macro is defined is public. As I said, "[...] the first of which should have the same public / private / protected status as the scope where it is defined", so either public or private or protected, depending on the scope.
Upvotes: 2
Views: 1764
Reputation: 234865
No there isn't. There are a few workarounds though:
Restrict your variables to a base class from which you inherit, and use your macros there. (Use protected
rather than private
).
Use an inner struct
adopting similar techniques to (1).
Do the pragmatic thing and have the macro set either the first or final block of members.
Don't use macros at all as, generally, they are a pain in the neck.
Upvotes: 6
Reputation: 18431
There is no inbuilt feature in language to do that. There are no push
and pop
settings kind of thing as with warning
and other aspects for pre-processor on some compilers.
The bigger question is Why? You declare class' skeleton and change it rarely later. It's not worth the effort. Also, it will make code less readable and complex to maintain.
Don't waste your time on it. I won't be productive or useful.
Upvotes: 1
Reputation: 10326
Well, if you assume that the 'default' scope is public, you could always change your macro to:
#define my_macro(a, b) int a;\
private: int b; \
public:
Or even:
#define my_macro(a, b) \
private: int b; \
public: int a;
Or if you're not sure what the default scope is, you can always add it as a macro parameter:
#define my_macro(scope, a, b) int a;\
private: int b; \
scope:
Upvotes: 1