Handy Hint or Shandy Shint...?

Started by
11 comments, last by LiquidNRG 20 years, 5 months ago
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]
Advertisement
Or you could just make the variable public since there is no error checking or validation or whatever!
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]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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.
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.
quote:Original post by YourOtherLeft

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


Uhm... how exactly does that work?

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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.
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?
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
In my opinion #defines are evil...they are useful when you really make code cleaner.
If you have a lot of rendundant code you can use templates...bad to write but very useful.

This topic is closed to new replies.

Advertisement