2
votes
    // MACROS

#define A_PROPERTY(TYPE, NAME)                      \
    private:                                        \
        TYPE NAME;                                  \
    public:                                         \
        void SetNAME(TYPE theNAME) {NAME=theNAME;}  \
        TYPE GetNAME() {return NAME;}               \

I am trying to creat a simple Macro that will take a TYPE and a NAME and will create:

  1. Private member of TYPE named NAME
  2. Public "getter" method called GetNAME (where NAME is variable...)
  3. Public "setter" method called GetNAME (where NAME is variable...)

This is how i'd use it from a class definition:

A_PROPERTY(int, Age)

The MACRO works, but creates a "getNAME" method instead of "get(whatever I put in NAME)". It seems MACROS don't parse strings and "parameters" together very well. Is there a way to achieve what I want? or should I just have the method as "NAME" with a different signature to differentiate the get and the set?

3
That is the most disgusting thing I have ever seen. - Puppy
Distasteful perhaps, but at least it's readable... - Richard
I hope I didn't cause you permanent damage, DeadMG... - JasonGenX
Ok, my curiosity overcame my sarcasm - what is so disgusting or distasteful about the above question? - JasonGenX

3 Answers

8
votes

Use ## to stitch tokens.

#define A_PROPERTY(TYPE, NAME)                            \
    private:                                              \
        TYPE NAME;                                        \ 
    public:                                               \
        void Set##NAME(TYPE the##NAME) {NAME=the##NAME;}  \
        TYPE Get##NAME() {return NAME;}
1
votes
void SetNAME(TYPE theNAME) {NAME=theNAME;}  
TYPE GetNAME() {return NAME;}              

This will create same function names for all NAME and TYPE parameters.

I think you want this:

void Set##NAME(TYPE the##NAME) {NAME=the##NAME;}  
TYPE Get##NAME() {return NAME;}               
0
votes

NAME isn't really private because you can read and write it via the member functions. You might as well use a struct, or make NAME public.