奇怪的班级宣言

The*_* do 7 c++ class

在Qt的qrect.h中,我发现类声明从这样开始:

class Q_CORE_EXPORT QRect {
};
Run Code Online (Sandbox Code Playgroud)

如您所见,类关键字后面有两个标识符.我怎么理解这个?
谢谢.

Mar*_*off 11

Q_CORE_EXPORT一个宏,它根据编译的上下文扩展到不同的值.

来自该来源的片段:

#ifndef Q_DECL_EXPORT
#  ifdef Q_OS_WIN
#    define Q_DECL_EXPORT __declspec(dllexport)
#  elif defined(QT_VISIBILITY_AVAILABLE)
#    define Q_DECL_EXPORT __attribute__((visibility("default")))
#  endif
#  ifndef Q_DECL_EXPORT
#    define Q_DECL_EXPORT
#  endif
#endif
#ifndef Q_DECL_IMPORT
#  ifdef Q_OS_WIN
#    define Q_DECL_IMPORT __declspec(dllimport)
#  else
#    define Q_DECL_IMPORT
#  endif
#endif

// ...

#    if defined(QT_BUILD_CORE_LIB)
#      define Q_CORE_EXPORT Q_DECL_EXPORT
#    else
#      define Q_CORE_EXPORT Q_DECL_IMPORT
#    endif
Run Code Online (Sandbox Code Playgroud)

这些值(__declspec(dllexport),__attribute__((visibility("default")))等)是特定于编译器的属性,表示动态库中函数的可见性.


MSa*_*ers 6

Q_CORE_EXPORT不是标识符.它是一个依赖于平台的宏,它用于表示要跨库边界使用的类.特别是,它由Qt核心库定义并由其他Qt库使用.