MathuSum Mut
MathuSum Mut

Reputation: 2825

Having trouble with macros in C++

I'm trying to write a C++ macro to define a bunch of sub-classes using a template and a color name like so:

#define DECLARE_SET_ELEMENT(color) class ##color##SetElement : public SetElement { public: ##color##SetElement(std::string name); int getValue() override; };

so that I can use it like:

DECLARE_SET_ELEMENT(Blue) // -> class BlueSetElement ...
DECLARE_SET_ELEMENT(Red)  // -> class RedSetElement ...
...

But the macro definition does not seem to be working correctly. How should it be in order for it to work as I am intending?

Upvotes: 0

Views: 54

Answers (1)

user3684240
user3684240

Reputation: 1590

Use

#define DECLARE_SET_ELEMENT(color) class color##SetElement : public SetElement { public: color##SetElement(std::string name); int getValue() override; };

instead. Leading ## are not suitable in this case. You don't want to combine the class keyword with color.

Upvotes: 3

Related Questions