|
-
June 21st, 2002, 04:59 PM
#1
token pasting / concatenation ( ## )
I am either doing this wrong or perhaps it's not possible.
I have some strings:
string m_sBlueOne;
string m_sBlueTwo;
string m_sRedOne;
string m_sRedTwo;
(etc.)
I want to create a macro to do the following:
When I call SWITCH_TO(Blue), for example , I want the following to occur:
strcpy(m_sBlueOne, "blah");
strcpy(m_sBlueTwo, "blah");
Since I intend to call SWITCH_TO(color) lots of times for different colors, I decided to be clever and write a quick macro.
[ccode]
#define SWITCH_TO( _color ) \
strcpy( m_s##_color##One, "blah"); \
strcpy( m_s##_color##Two, "blah");
[/ccode]
But of course it doesn't work... My idea was that the concatenation would simply put my _color parameter into the variable name, but perhaps you can't use the ## operator to append anything but tokens? (i.e., you can't append the "One" and "Two" strings?)
Any ideas?
-J
-
June 21st, 2002, 05:50 PM
#2
Why "of course, it doesn't work"?
I just tried
Code:
#define CONCAT(x) int m_s##x##thing
int main()
{
CONCAT(blah);
}
and the preprocessor output for that is:
Code:
#line 1 "d:\\cpp\\blah\\blah.cpp"
int main()
{
int m_sblahthing;
}
which looks right to me.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 22nd, 2002, 05:30 PM
#3
d'oh
I just checked my code, seems I was missing an underscore.
Such that I had this:
#define SWITCH(_color_) \
m_s##_color##String = "blah";
Guess it's time to switch to a larger font.... sigh... how does that song go? "What a drag it is getting old..."
Thanks for checking with code of your own.
-J
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|