在 C++ 类中使用 dllimport 和 dllexportUsing dllimport and dllexport in C++ Classes

11/04/2016

本文内容

Microsoft 专用Microsoft Specific

可以用或属性声明 c + + 类 dllimport dllexport 。You can declare C++ classes with the dllimport or dllexport attribute. 这些形式表示已导入或导出整个类。These forms imply that the entire class is imported or exported. 以这种方式导出的类称为可导出类。Classes exported this way are called exportable classes.

以下示例定义了可导出类。The following example defines an exportable class. 将导出其所有成员函数和静态数据:All its member functions and static data are exported:

#define DllExport __declspec( dllexport )

class DllExport C {

int i;

virtual int func( void ) { return 1; }

};

请注意, dllimport dllexport 禁止对可导出类的成员显式使用和属性。Note that explicit use of the dllimport and dllexport attributes on members of an exportable class is prohibited.

dllexport 类dllexport Classes

声明类时 dllexport ,将导出其所有成员函数和静态数据成员。When you declare a class dllexport, all its member functions and static data members are exported. 您必须在同一程序中提供所有此类成员的定义。You must provide the definitions of all such members in the same program. 否则,将生成链接器错误。Otherwise, a linker error is generated. 此规则有一个例外情况,即对于纯虚函数,您无需为其提供显式定义。The one exception to this rule applies to pure virtual functions, for which you need not provide explicit definitions. 但是,由于基类的析构函数始终在调用抽象类的析构函数,因此纯虚拟析构函数必须始终提供定义。However, because a destructor for an abstract class is always called by the destructor for the base class, pure virtual destructors must always provide a definition. 请注意,这些规则对不可导出的类是相同的。Note that these rules are the same for nonexportable classes.

如果导出类类型的数据或返回类的函数,请务必导出类。If you export data of class type or functions that return classes, be sure to export the class.

dllimport 类dllimport Classes

声明类时 dllimport ,会导入其所有成员函数和静态数据成员。When you declare a class dllimport, all its member functions and static data members are imported. 与 dllimport 非类类型上和的行为不同 dllexport ,静态数据成员不能在定义类的同一程序中指定定义 dllimport 。Unlike the behavior of dllimport and dllexport on nonclass types, static data members cannot specify a definition in the same program in which a dllimport class is defined.

继承和可导出类Inheritance and Exportable Classes

可导出类的所有基类都必须是可导出的。All base classes of an exportable class must be exportable. 否则,会生成编译器警告。If not, a compiler warning is generated. 此外,同样是类的所有可访问成员必须是可导出的。Moreover, all accessible members that are also classes must be exportable. 此规则允许 dllexport 类从类继承 dllimport ,使用类 dllimport 从类继承, dllexport 但不建议将后者) (。This rule permits a dllexport class to inherit from a dllimport class, and a dllimport class to inherit from a dllexport class (though the latter is not recommended). 通常来说,对 DLL 客户端可访问的所有内容(根据 C++ 访问规则)都应该是可导出接口的一部分。As a rule, everything that is accessible to the DLL's client (according to C++ access rules) should be part of the exportable interface. 这包括在内联函数中引用的私有数据成员。This includes private data members referenced in inline functions.

选择性成员导入/导出Selective Member Import/Export

由于类中的成员函数和静态数据隐式具有外部链接,因此你可以使用 dllimport 或特性声明它们 dllexport ,除非导出整个类。Because member functions and static data within a class implicitly have external linkage, you can declare them with the dllimport or dllexport attribute, unless the entire class is exported. 如果导入或导出整个类,则禁止将成员函数和数据显式声明为 dllimport 或 dllexport 。If the entire class is imported or exported, the explicit declaration of member functions and data as dllimport or dllexport is prohibited. 如果在类定义中将静态数据成员声明为 dllexport ,则定义必须出现在同一程序中的某处 (与非类外部链接) 相同。If you declare a static data member within a class definition as dllexport, a definition must occur somewhere within the same program (as with nonclass external linkage).

同样,可以用或特性声明成员函数 dllimport dllexport 。Similarly, you can declare member functions with the dllimport or dllexport attributes. 在这种情况下,您必须在 dllexport 同一程序中的某处提供定义。In this case, you must provide a dllexport definition somewhere within the same program.

有关选择性成员导入和导出的某些要点值得注意:It is worthwhile to note several important points regarding selective member import and export:

选择性成员导入/导出最适合用于提供具有更强限制的导出类接口版本;即,您可以为该版本设计一个 DLL,该 DLL 公开的公用和专用功能比本应允许的语言公开的更少。Selective member import/export is best used for providing a version of the exported class interface that is more restrictive; that is, one for which you can design a DLL that exposes fewer public and private features than the language would otherwise allow. 这对于优化可导出接口也很有用:当通过定义知道客户端无法访问某些私有数据时,您不需要导出整个类。It is also useful for fine-tuning the exportable interface: when you know that the client, by definition, is unable to access some private data, you need not export the entire class.

如果导出了某个类中的一个虚函数,则必须导出其中的所有虚函数,或者至少必须提供客户端可直接使用的版本。If you export one virtual function in a class, you must export all of them, or at least provide versions that the client can use directly.

如果有在其中将选择性成员导入/导出用于虚函数的类,则这些函数必须在可导出接口或已定义内联中(对客户端可见)。If you have a class in which you are using selective member import/export with virtual functions, the functions must be in the exportable interface or defined inline (visible to the client).

如果将成员定义为 dllexport ,但不将其包括在类定义中,则会生成编译器错误。If you define a member as dllexport but do not include it in the class definition, a compiler error is generated. 必须在类头中定义成员。You must define the member in the class header.

尽管允许将类成员定义为 dllimport 或 dllexport ,但不能重写在类定义中指定的接口。Although the definition of class members as dllimport or dllexport is permitted, you cannot override the interface specified in the class definition.

如果在声明的类定义的主体以外的位置定义成员函数,并且 dllexport dllimport 如果此定义与类声明) 中指定的不同,则会生成警告(如果该函数定义为或 ()。If you define a member function in a place other than the body of the class definition in which you declared it, a warning is generated if the function is defined as dllexport or dllimport (if this definition differs from that specified in the class declaration).

结束 Microsoft 专用END Microsoft Specific

请参阅See also

不能定义声明dllimport_在 C++ 类中使用 dllimport 和 dllexport相关推荐

  1. 不能定义声明dllimport_不允许 dllimport 静态数据成员

    View Code "CTest::~CTest" : 不允许 dllimport 函数 的定义 "CTest::CTest" : 不允许 dllimport ...

  2. 不能定义声明dllimport_DllImport与VB.NET中的声明

    显然,Declare和DllImport语句基本相同.你可以使用任何你喜欢的. 以下是关于每一点可能有点不同的几点的讨论,这可能会影响到彼此的偏好: 我从MSDN的一篇关于Visual Studio ...

  3. 不能定义声明dllimport_不允许 dllimport 函数 的定义 高手帮帮忙

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 我的代码声明: #include "stdafx.h" #include "ImageHandle.h" #inc ...

  4. c++为什么一定要定义无参构造函数?类中无参构造的作用

    c++中无参构造函数的作用: 该类对象被创建时,编译系统为对象分配内存空间,并自动调用该构造函数->由构造函数完成成员的初始化工作. 故:构造函数的作用:初始化对象的数据成员. 举例: clas ...

  5. 不能定义声明dllimport_不允许 dllimport 函数 的定义

    我的代码声明:#include"stdafx.h"#include"ImageHandle.h"#include"CustomMenu.h" ...

  6. java静态方法声明_方法本地类中的Java最终静态声明

    在方法内部声明局部内部类时,为什么包含最终的静态String或int是合法的,而包含其他对象却不合法? 例如: class Outer { void aMethod() { class Inner { ...

  7. c++学习笔记之基础---类内声明函数后在类外定义的一种方法

    在C++的"类"中经常遇到这样的函数, 返回值类型名 类名::函数成员名(参数表){ 函数体.} 双冒号的作用 ::域名解析符!返回值类型名 类名::函数成员名(参数表) { 函数 ...

  8. 【Groovy】闭包 Closure ( 闭包调用 与 call 方法关联 | 接口中定义 call() 方法 | 类中定义 call() 方法 | 代码示例 )

    文章目录 总结 一.接口中定义 call() 方法 二.类中定义 call() 方法 三.完整代码示例 总结 在 实例对象后使用 " () " 括号符号 , 表示调用该实例对象的 ...

  9. 4.3调整基类成员在派生类中的访问属性的方法

    同名成员 在定义派生类的时候,C++语言允许派生类与基类中的函数名相同.如果在派生类中定义了与基类中相同的成员,则称派生类成员覆盖了基类的同名成员,在派生类中使用这个名字意味着访问在派生类中重新说明的 ...

最新文章

  1. [转]Asp.Net 上传大文件专题(3)--从请求流中获取数据并保存为文件[下]
  2. 【设计模式】—— 命令模式Commond
  3. rk android8.1加密,Android 8.1RK平台增加自定义脚本,修改文件权限
  4. 你有没有遇到要实现多种登录方式的场景丫 一起来看看咯 Spring Security 实现多种登录方式,如常规方式外的邮件、手机验证码登录
  5. nginx location 匹配 多个规则_你需要知道的Nginx配置二三事
  6. sytlengan2 代码解析
  7. 光耦驱动单向可控硅_双向晶闸管的触发用的光耦驱动mos桥,光耦
  8. Atitit 高性能架构法艾提拉著作 目录 1. 前期可以立即使用的技术 2 2. 分离法 3 2.1. Web db分离 3 2.2. 读写分离 4 2.3. CDN加速技术 4 2.4. 动静分
  9. 川大计算机学硕调剂专硕,19双非考研川大计算机学院专硕经验~
  10. 破解jQuery插件收费、下载币(单页扒站小工具)
  11. Python期末考试
  12. angular- Directive
  13. Fisher information(费雪信息)和费雪信息矩阵
  14. 听说一个漂亮的小姐姐图片是程序员无法抗拒的
  15. 技术团队里什么样的人会被清除?抢老板的工作干合适吗?
  16. 用计算机打山有木兮,山有木兮 - 在线打字测试(dazi.kukuw.com)
  17. 德鲁依历史全记录—异民族魔法师
  18. H - 悼念512汶川大地震遇难同胞——一定要记住我爱你
  19. Mysql基于binlog日志恢复数据
  20. Kafka 的这些原理你知道吗

热门文章

  1. 2023天猫运营数据分析:Q1防晒品类行业分析报告
  2. 虚拟现实技术在神经外科教学中的应用
  3. SWD和JTAG调试接口
  4. Python 学习笔记之 networkx 使用
  5. RBF Kernel 是一种度量的证明
  6. perspective java_eclipse创建Java项目时提示Open Associated Perspective?
  7. dart-sass与node-sass介绍
  8. Internet宽带接入方式详解
  9. 最优化方法 ——— 一阶优化
  10. java面试:冒泡排序详解 (Java经典编程案例)