evk1206
evk1206

Reputation: 463

Class in C++ using Macros before Classname

I have the following doubt on class declaration in C++. Why this kind of declaration is required?

class MACRONAME cStream : public comm::IStream

Why a macro is included in class name. Is there any advantage of this declaration?

Upvotes: 0

Views: 658

Answers (1)

kola
kola

Reputation: 340

Usually it stands for __declspec(dllexport) and __declspec(dllimport) (see https://msdn.microsoft.com/en-us/library/dabb5z75.aspx). Which makes class definition exported when building a dll, and imported when building something that references the dll.

For example in some common header of a dll project there is someting like this:

#if defined( _BUILDING_MY_PROJECT_ )
  #define MY_PROJECT__TYPE __declspec(dllexport)
#else
  #define MY_PROJECT__TYPE __declspec(dllimport)
#endif  

And a project defines a _BUILDING_MY_PROJECT_ macro so classes taged with MY_PROJECT__TYPE will be built with __declspec(dllexport) and in other cases when the header is included in the sources of another project class definition will taged __declspec(dllimport).

And it makes it easier to keep code more flexible, for example on non windows platforms such macro can be defined empty.

Upvotes: 1

Related Questions