Jump to content
  • Advertisement
Sign in to follow this  
d000hg

Macro question (C++)

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

I have a macro which is something like this:
#define CREATE_FUNCTOR(name,method) struct name {  void method(); };
This isn't exact so don't focus on why I want to do this! My problem is that I want the struct to be called _name, not name. If I change the macro to be:
#define CREATE_FUNCTOR(name,method) struct _name {  void method(); };
Then I get a struct called "_name". Is it possible to get the preproccesor to understand I want to stick '_' onto the front of the variable? (It's actually for a message handling thing and I want to create a functor called _OnXXX for each event-handler OnXXX)

Share this post


Link to post
Share on other sites
Advertisement
Thanks guys. I agree it's ugly to use macros for this at all but I don't think C++ makes what I want possible through normal means (I'm making something a little like MFC).

Share this post


Link to post
Share on other sites
It is in particular naughty to just use an underscore, though, because it is extremely likely that someone using your framework will want to use that name. Don't make it _whatever. At the very least, make it __AUTO__d000hgsframework__whatever or suchlike.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
It is in particular naughty to just use an underscore, though, because it is extremely likely that someone using your framework will want to use that name. Don't make it _whatever. At the very least, make it __AUTO__d000hgsframework__whatever or suchlike.


He should be careful with the CREATE_FUNCTOR identifier as it's a macro, but the _name identifier is fine, as it obeys scoping rules.

Also, the identifier __AUTO__d000hgsframework__ is reserved to the compiler and standard library.

Share this post


Link to post
Share on other sites
Quote:
Original post by d000hg
I have a macro which is something like this:
#define CREATE_FUNCTOR(name,method) struct name {  void method(); };

This isn't exact so don't focus on why I want to do this!


Sorry, I must insist. You are aware of std::mem_fun and std::mem_fun_ref, yes? I'm pretty sure there's *something* that solves your problem more nicely...

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Sorry, I must insist. You are aware of std::mem_fun and std::mem_fun_ref, yes? I'm pretty sure there's *something* that solves your problem more nicely...


And if those don't do what you need, how about boost::function and boost::bind?
I can guarrentee they will do more than you can do with your macros [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
[...]
Also, the identifier __AUTO__d000hgsframework__ is reserved to the compiler and standard library.


</sarcasm>.

May I conclude that you think using identifiers like this is OK, in spite of the holy?

To come out, yes, I use #ifndef __MYFREAKINGFILENAME_H... all the time. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
Quote:
Original post by Nitage
[...]
Also, the identifier __AUTO__d000hgsframework__ is reserved to the compiler and standard library.


</sarcasm>.

May I conclude that you think using identifiers like this is OK, in spite of the holy?

To come out, yes, I use #ifndef __MYFREAKINGFILENAME_H... all the time. [smile]


Why type 2 extra characters just to risk breaking your program? [smile]

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!