Click to See Complete Forum and Search --> : token pasting / concatenation ( ## )


jmaton
June 21st, 2002, 04:59 PM
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.


#define SWITCH_TO( _color ) \
strcpy( m_s##_color##One, "blah"); \
strcpy( m_s##_color##Two, "blah");


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

Graham
June 21st, 2002, 05:50 PM
Why "of course, it doesn't work"?

I just tried

#define CONCAT(x) int m_s##x##thing

int main()
{
CONCAT(blah);
}


and the preprocessor output for that is:


#line 1 "d:\\cpp\\blah\\blah.cpp"


int main()
{
int m_sblahthing;
}


which looks right to me.

jmaton
June 22nd, 2002, 05:30 PM
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