Jump to content
• Advertisement

# #define and ##name##

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

##### Share on other sites
Advertisement
Quote:
 Original post by SpreeTree...add access functions for all his private variables.

Eh.. Nice design..

#### Share this post

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by SpreeTreeNow 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

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

##### Share on other sites
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!

#### Share this 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

##### Share on other sites
Quote:
 Original post by Daniel MillerLooking 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

##### Share on other sites
Quote:
 Original post by FrunyWriting 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 MillerLooking 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 SpreeTreeI 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

##### Share on other sites
Quote:
 Original post by SpreeTreeI 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5
• Advertisement

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632650
• Total Posts
3007645
• ### Who's Online (See full list)

There are no registered users currently online

×

## 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!