Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the #define macro to standardize class declaration

I'm building a C++ DLL for one of my projects. I am trying to standardize the way that are class are defined. So instead of each time writing:

class __declspec(dllexport) ClassName

I'm building a #define macro to ease this process:

#define CLASS( cName ) class __declspec(dllexport) cName

But, when I'm using it, it gives me the following error:

Error: Expected a ';'

I know you can use a #define macro to define an entire class creation, but can it be used to define only the "class header" ?

Thanks,

Keep in mind that I'm trying to do so because we are going to deal with hundreds of classes, so these kinds of "automation" would be most helpful :)

EDIT:

example:

#define CLASS( nClass ) class __declspec(dllexport) nClass

CLASS( APTest )
{                        // Here is the error of missing ';'
public:
    APTest();
};
like image 733
TurnsCoffeeIntoScripts Avatar asked Dec 04 '22 11:12

TurnsCoffeeIntoScripts


1 Answers

Don't do this.

C++ has already been standardized!

If you ever expect other people to read your code then just write it in conventional C++, not some homecooked dialect that looks different. Get used to the proper C++ syntax, it will make it easier to read other people's C++ code.

One thing that does make sense is to simplify the __declspec part, which you can do like this:

#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport) 
#else
#define DLLEXPORT
#endif

class DLLEXPORT APTest
{
    // ...
};

You're really not making your life any simpler by writing CLASS( APTest ) and you make it harder for others to understand. Just say no.

like image 143
Jonathan Wakely Avatar answered Dec 09 '22 14:12

Jonathan Wakely