#define and ##name##

Started by
18 comments, last by Sneftel 18 years, 9 months ago
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
Advertisement
Quote:Original post by SpreeTree
...add access functions for all his private variables.


Eh.. Nice design..
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
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.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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 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
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
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.
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.

This topic is closed to new replies.

Advertisement