Tag Archives: string

[Coketorial C++] macro: transform the parameter into a string

A while ago I wanted to create a macro that would transform the parameter intr-o a character string, something like:

CString strString = MACRO(666;puppies); // => strString = “666;puppies”

I wanted to do so because I was going to create another, more complex macro that would declare/define some functions for classes, without having to take time for each class and implementing the functions manually. Well, that’s how I found out that in order to transform a macro’s parameter into a character string, you need, in fact, 2 macros, as follows:

#define Intermediary(parameter) #parameter
#define StringMachine(anyparameter) Intermediary(anyparameter)

That simple. That’s how I moved on and created another macro that would create a function that returned the class’s name:

#define DEFINE_CLASS(className) public:
virtual CString name() const { return StringMachine(className); };

Now I’m using this macro inside each class in order to be able to get at any time the name of the class. Something like:

class ClassName




, a thing that makes my work a lot easier because I only have to write 3 words instead of tens (depends on the complexity of the DEFINE_CLASS macro).

For the program to work (if you use CString, that is) you must also include atlstr.h. For more information about CString go here.

If you have questions, don’t hestitate to post a comment.