Jump to content
  • Advertisement

Archived

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

LiquidNRG

Handy Hint or Shandy Shint...?

This topic is 5444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
quote:
Original post by YourOtherLeft

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



Uhm... how exactly does that work?

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
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!