Jump to content
  • Advertisement
Sign in to follow this  
SpreeTree

#define and ##name##

This topic is 4724 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

A friend of mine uses the syntax below to cut down on the number of code lines he has to write to add access functions for all his private variables.
#define GETFLOAT(NAME) float Get##NAME##(void) { return m_f##NAME##; } }
Now this seems to work quite well, and as long as it is kept simple, a real time saver. I wondered if anyone else had used this style of #define'ing before, and what their thoughts were on it? Also (and most important) is this a .net or MS specific peice of code, and will it work on Linux and Mac based systems? Thanks Spree

Share this post


Link to post
Share on other sites
Advertisement
Not for getters and setters. If I even need more than a few then I've generally done something wrong and need to redesign it anyway.
However I do use macros and name concatenation freely to cut down on repetitive code, especially for data initialization.

Unless you happen to encounter a pre-ANSI C preprocessor I wouldn't worry about compability.

Share this post


Link to post
Share on other sites
Quote:
Original post by SpreeTree
Now this seems to work quite well, and as long as it is kept simple, a real time saver.


Writing a macro for something that simple really isn't a good idea.

Quote:
I wondered if anyone else had used this style of #define'ing before, and what their thoughts were on it?


I have used macros to produce definitions, but they were rather more intricate than that one. Honestly, I don't think much good of code like your friend's.

Quote:
Also (and most important) is this a .net or MS specific peice of code, and will it work on Linux and Mac based systems?


It's pure C preprocessor stuff. It will work ony any system that provides one such. Which in fact is generally more likely on Linux and MacOS X than it is on Windows, since those two platform typically ship with a compiler (GCC) while Windows ... does not.

Share this post


Link to post
Share on other sites
Do you need getters for *all* your private variables? And if you do, how often is it always a trivial getter like that?

If the answer is yes to both of these questions, your class design probably isn't much good.

Share this post


Link to post
Share on other sites
I used the above example as it was short and sweet, and showed the feature that I wanted to discuss.

I personally am not using it for getters and setters, instead using it for a chunk of code that I have to write time and time again, for different class types and variables (and no templates will not solve that problem:)

I just wanted peoples general opinion on the style of coding using that, and if (other than the usual macro problems) their are any particular dangers with the code.

Spree

Share this post


Link to post
Share on other sites
Quote:
Original post by Daniel Miller
Looking at his code (void return type, f prefix) it seems that he still thinks he's in C. Point him in the direction of a good C++ article!


If you look closer at the #define, it is actually a float return type, and the f prefix is a coding notation used for this particular piece of code. It has nothing to do with C or C++

Spree

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Writing a macro for something that simple really isn't a good idea.
I disagree. I regularly use macros to cut down on large chunks of very simple but highly repetitive code.

Quote:
Original post by Daniel Miller
Looking at his code (void return type, f prefix) it seems that he still thinks he's in C. Point him in the direction of a good C++ article!
I assume you're refering to the explicitly void parameter list.
Personally I'm in the habit of always writing out (void). Since it doesn't hurt in C++ while using a truly empty declaration in C is equivalent to a function with a variable argument list.
It's just way to easy to forget to switch back to the old style when using C since most compilers don't give you any warnings and gladly accept arguments to void functions. In fact I used a library where the author had forgotten about this just a few days ago.

Quote:
Original post by SpreeTree
I personally am not using it for getters and setters, instead using it for a chunk of code that I have to write time and time again, for different class types and variables (and no templates will not solve that problem:)

I just wanted peoples general opinion on the style of coding using that, and if (other than the usual macro problems) their are any particular dangers with the code.
Sure, as long as it saves time in the long run and doesn't obfuscate things too much.

Share this post


Link to post
Share on other sites
Quote:
Original post by SpreeTree
I just wanted peoples general opinion on the style of coding using that, and if (other than the usual macro problems) their are any particular dangers with the code.


Debugging code generated by your preprocessor macros is going to be a pain in the arse.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!