C++ method declaration including a macro

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 04:37:11

问题


I'm using QuickFAST library and while checking it I found this class declaration which I don't seem to really get ! I mean what does a macro name before the class name !

class QuickFAST_Export Message : public FieldSet

also I found this declaration

friend void QuickFAST_Export intrusive_ptr_add_ref(const Field * ptr);

and again I don't get the use of this declaration !

for more info here's the QuickFAST_Export.hpp

#ifdef _MSC_VER
# pragma once
#endif
#ifndef QUICKFAST_EXPORT_H
#define QUICKFAST_EXPORT_H

// Compile time controls for library generation.  Define with /D or #define
// To produce or use a static library: #define QUICKFAST_HAS_DLL=0
//   Default is to produce/use a DLL
// While building the QUICKFAST_ library: #define QUICKFAST_BUILD_DLL
//   Default is to export symbols from a pre-built QUICKFAST DLL
//
// Within QUICKFAST use the QuickFAST_Export macro where a __declspec is needed.

#if defined (_WIN32)

#  if !defined (QUICKFAST_HAS_DLL)
#    define QUICKFAST_HAS_DLL 1
#  endif /* ! QUICKFAST_HAS_DLL */

#  if defined (QUICKFAST_HAS_DLL) && (QUICKFAST_HAS_DLL == 1)
#    if defined (QUICKFAST_BUILD_DLL)
#      define QuickFAST_Export __declspec(dllexport)
#    else /* QUICKFAST_BUILD_DLL */
#      define QuickFAST_Export __declspec(dllimport)
#    endif /* QUICKFAST_BUILD_DLL */
#  else /* QUICKFAST_HAS_DLL == 1 */
#    define QuickFAST_Export
#  endif /* QUICKFAST_HAS_DLL == 1 */

#  else /* !_WIN32 */

回答1:


It means that the class is either exported or imported, depending on which module is built.

If QUICKFAST_HAS_DLL is defined and equal to 1, it means that the module is built as a DLL. To use functionalities from the outside, classes and methods have to be exported.

Inside the module, QUICKFAST_BUILD_DLL is defined. So when you build the module, QuickFAST_Export expands to __declspec(dllexport). Your class definition becomes:

class __declspec(dllexport) Message : public FieldSet

When you include the header from a different module, QUICKFAST_BUILD_DLL is not defined, so the macro expands to __declspec(dllimport), and your class definition to:

class __declspec(dllimport) Message : public FieldSet



回答2:


The macro expands to either __declspec(dllimport) or __declspec(dllexport), depending if the class is exported from the DLL or imported on the other side.



来源:https://stackoverflow.com/questions/10210449/c-method-declaration-including-a-macro

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