What does this kind of C++ class declaration mean?

我们两清 提交于 2019-12-02 08:30:37

问题


I downloaded Ogre3D source code, and found this kind of class declaration:

class _OgreExport TimeIndex
{ ...

I know "TimeIndex" is the class name, but what is the "_OgreExport" in the middle? CPP reference doesn't include this kind of class declaration form. What is this?


回答1:


_OgreExport is a preprocessor directive that expands to either

__declspec(dllimport)

when the file is included outside its module or

__declspec(dllexport)

otherwise. Under Windows, you have to specify which classes/methods you want exported/imported so that they can be used across binaries.

Technically, as James pointed out in the comments, the macro name is illegal, since it begins with an underscore. These names are reserved for the implementation.




回答2:


see this code from OgrePlatform.h:138

#       if defined( OGRE_NONCLIENT_BUILD )
#           define _OgreExport __declspec( dllexport )
#       else
#           if defined( __MINGW32__ )
#               define _OgreExport
#           else
#               define _OgreExport __declspec( dllimport )
#           endif
#       endif
#       define _OgrePrivate
#   endif

I highly recommend using google code search if you have further questions of this type. Just enter, e.g., _OgreExport and see how other used it or how it is defined.




回答3:


It's a macro that expands to something like __declspec(dllexport), marking the class to be exported by the linker.



来源:https://stackoverflow.com/questions/14828700/what-does-this-kind-of-c-class-declaration-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!