I have a bunch of classes such as the following,
class SomeClass : public Function{
public:
ref call(ref args){
// do & return stuff
}
int getType(){return TYPE;}
ref toString(){ return "SomeClass";}
};
I got like 50 of these and the only thing that is different is the body of the call function. Is it possible to have a macro that will take a name and a body and replace “SomeClass” with name and insert body into call function?
Sure. Expanding the body of the
callmember function is a bit easier if you have a compiler that supports variadic macros. While I’ve used Boost.Preprocessor’s stringize macro, it is trivial to write your own.Used as:
The
callbody needs to be parenthesized so that any commas present in the body are not treated as macro argument separators. Alternatively, you could just declare thecallfunction in the class definition and then define that function separately.