Archived

This topic is now archived and is closed to further replies.

LiquidNRG

Handy Hint or Shandy Shint...?

Recommended Posts

Just a bit of coding practice I came across on my travels and thought I would share it. You may have seen it before but for those who haven't
#define DEFINE_VARIABLE(type,var)
    public:
        type Get##var() const { return var; }
        void Set##var(const type &val) { this->val ;}       
    protected:                                                  
        type var;
Oh yes you must remember to put a \ at the end of all the lines except the last one this is as it is a macro definition and thus is effectively a single compile line in the compilers eyes. Then simply use as DEFINE_VAR(int,hello) and you will have a variable which is used through the Gethello() and Sethello() functions. [edited by - YourOtherLeft on October 23, 2003 6:41:25 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Or you could just make the variable public since there is no error checking or validation or whatever!

Share this post


Link to post
Share on other sites
Just don't.

I find it exceedingly rare that data members in even 10% of my classes actually need the get/set member functions. I don't even write them until the need comes up.

[edited by - antareus on October 23, 2003 9:47:54 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Or you could just make the variable public since there is no error checking or validation or whatever!


True, although at least with this sort of approach you define an interface which allows you to perform checking or special handling in the future, if you need it, without completely breaking all the code that depends on it.

However, as antereus says, you shouldn''t need to write lots of accessors for your classes if you are encapsulating them properly.

Share this post


Link to post
Share on other sites
Yea i have to agree this macro isnt terribly useful to me, although it is kind of neat.

I have seen some extremely cool macro thingys before though. One of my favorite (although not the cleverest, but still up there), is the one where you define a LOG macro.

you can use it like this:
LOG("hi there")
and it outputs the text to a file, along with the time, and the file name and line and other cool stuff.

Share this post


Link to post
Share on other sites
Yeah I know there is no error checking stuff etc and I didnt expect many people to have use for it but I thought I would share cos I thought is was pretty neat and someone may get somethign from it.

Share this post


Link to post
Share on other sites
quote:
Original post by superpig
quote:
Original post by YourOtherLeft

void Set##var(const type &val) { this->val ;}



Uhm... how exactly does that work?


When you use DEFINE_VAR(Name) you will see in the definition that the protected member is called ''var'' the ##var tells the compiler that when the is replaced ie GetName() it looks at the variable defined Name. Not sure how better to describe it, any offers?

Share this post


Link to post
Share on other sites
A few macros I''ve come across were neat. Seems there was an include file somewhere that declared a bunch of macros that allowed you to write in a BASIC-like syntax, which would be compiled normally.

Not terribly useful for me, but fun nonetheless.


daveandrews.org - a Christian Programmer''s Weblog

Share this post


Link to post
Share on other sites
quote:
Original post by YourOtherLeft
quote:
Original post by superpig
quote:
Original post by YourOtherLeft

void Set##var(const type &val) { this->val ;}



Uhm... how exactly does that work?


When you use DEFINE_VAR(Name) you will see in the definition that the protected member is called ''var'' the ##var tells the compiler that when the is replaced ie GetName() it looks at the variable defined Name. Not sure how better to describe it, any offers?



No, no, the token-pasting stuff is fine - I use it all the time - but when you actually use the macro, looks what happens:


//here''s the original macro

#define DEFINE_VARIABLE(type,var)
public:
type Get##var() const { return var; }
void Set##var(const type &val) { this->val ;}
protected:
type var;

//here''s me using it

DEFINE_VARIABLE(int, Foo)

//here''s the output from the preprocessor

public:
int GetFoo() const { return foo; }
void SetFoo(const int &val) { this->val; }
protected:
int foo;


Now please tell me, what does the SetFoo function actually do, hmm?

Also, are you sure you don''t have to use the token-pasting operator a bit more? I''d probably write that macro like:



#define DEFINE_VARIABLE(type,var)
public:
type Get##var##() const { return ##var##; }
void Set##var##(const type &val) { this->val ;}
protected:
type var##;



I''m not sure what the rules are about trailing characters on tokens in macros...

Richard "Superpig" Fine
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .y''ybu. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu
OpenGL is a language

Share this post


Link to post
Share on other sites
That macro would probably destroy Visual Studio''s autocomplete. You''d get your class and then DEFINE_VARIABLE in the menu. Yeah, exactly what you wanted!

At least that has been my experience with macros in class headers.

Share this post


Link to post
Share on other sites
I hate macros that change the accessor rules for a given class, they lead to hard-to-spot errors. I also strongly dislikes get/set-methods that provides no check on the data, then I rather keep my data public.
Sorry, but I just don''t think this macro is usable at all.
Macros are not your best friend, see them more as the kind of (clumsy?) friend who once or twice gets you out of a tight spot, but most of the time really makes your life a hell (because they screw things up).

Share this post


Link to post
Share on other sites