Macro Class. What!?

Yeah, you heard it right, Macro Class! What in the world. When I was working as an intern, I found out that Macro Class is a painless way to "generate" a class for you without any code. As you probably know, macro is a way that you can use to tell the compiler to replace some strings for you. When you do this:


#define VALUE 5

The compiler will go over your code and replace all the occurrences of VALUE (except when VALUE is inside a string) with the number 5. Now you can do that to a class. Think about times when you want to write a class without having to write a class. This guy isn't serious... Here's how:


#define ClassGenerator(_class_name_)\
class _class_name_ {\
public: \
    _class_name_() {}\
    ~_class_name_() { }\
    std::string getResult();\
};
He IS serious..

Now what can I do with this? A lot of thing!!! You can then use your newly created macro to create your class with a getResult() function. Wait, I need a declaration for this function, right? Here's how:


ClassGenerator(SomeClass)
std::string SomeClass::getResult()
{
   return "Wicked is good.";
}

So what? So now you don't have to write ten thousand classes. All is taken care by the macro itself. To define your new class, simply do: ClassGenerator(SomeClass)

Note: If you look at the STD library, you will see people use this method a lot. This is cool, isn't it?

Comments

Popular posts from this blog

Markdown vs. the World

New Divide - Linkin Park (Official Music Video)