编译器警告(等级 1)C4251Compiler Warning (level 1) C4251

'类型' : 类 '类型 1' 需要有 dll 接口,以便类 '类型2' 的客户端使用 dll 接口'type' : class 'type1' needs to have dll-interface to be used by clients of class 'type2'

备注Remarks

为了在导出声明为__declspec(dllexport)的类时,将数据损坏的可能性降至最低,请确保:To minimize the possibility of data corruption when exporting a class declared as __declspec(dllexport), ensure that:

  • 所有静态数据都通过从 DLL 导出的函数进行访问。All your static data is accessed through functions that are exported from the DLL.

  • 类的内联方法无法修改静态数据。No inlined methods of your class can modify static data.

  • 类中联方法不使用 CRT 函数或使用静态数据的其他库函数。No inlined methods of your class use CRT functions or other library functions that use static data. 有关详细信息,请参阅通过DLL 边界传递 CRT 对象的潜在错误For more information, see Potential errors passing CRT objects across DLL boundaries.

  • 类(无论是否内联)的函数中任何方法都无法使用 EXE 和 DLL 中的实例化具有静态数据差异的类型。No methods of your class (whether inlined or not) can use types where the instantiation in the EXE and DLL have static data differences.

在从 DLL 导出类时,可以避免问题:将类定义为具有虚拟函数,以及实例化和删除类型对象的函数。You can avoid issues when exporting a class from a DLL: Define your class to have virtual functions, and functions to instantiate and delete objects of the type. 然后,您可以调用类型的虚拟函数。You can then just call virtual functions on the type.

如果类派生自标准库中C++类型,编译调试版本 (/MTd), 以及编译器错误消息引用_Container_base的位置,则可以忽略 C4251。C4251 can be ignored if your class is derived from a type in the C++ Standard Library, you're compiling a debug release (/MTd), and where the compiler error message refers to _Container_base.

示例Example

此示例导出派生自VecWrapper``std::vector的专用类。This sample exports a specialized class VecWrapper derived from std::vector.

// C4251.cpp
// compile with: /EHsc /MTd /W2 /c
#include <vector>
using namespace std;
class Node;
class __declspec(dllexport) VecWrapper : vector<Node *> {};   // C4251