导出类(DLL)中静态数据成员的可访问性?

时间:2012-02-16 22:19:19

标签: c++ visual-c++ dll dllimport dllexport

假设我有这门课程:

class __declspec(dllexport) MyClass
{
  public:
    static int Bar;
    static MyOtherClass Foo;
  private:
    static int OtherStuff;
};

我有一些问题(我使用的是MSVC编译器):

  1. 导入此类的客户端是否可以访问静态成员“Bar”?
  2. 静态成员“OtherStuff”也会被导出吗?如果不是,这是由于访问修饰符private:
  3. 如果未使用MyOtherClass定义类__declspec(dllexport),我相信这意味着MSVC编译器会发出警告C4251,但这是否意味着变量Foo导入此类的客户端无法访问?
  4. 我基本上只是在我的脑海中运行各种场景,试图在静态数据成员方面找出DLL类接口中什么是未导出(以及因此无法访问)。

1 个答案:

答案 0 :(得分:0)

代码:

class MyOtherClass
{
public:
    int something;
};

class __declspec(dllexport) MyClass
{
  public:
    static int Bar;
    static MyOtherClass Foo;
  private:
    static int OtherStuff;
};

int MyClass::Bar = 0;
MyOtherClass MyClass::Foo;
int MyClass::OtherStuff = 0;

我在Dependency Walker中得到以下内容:

class MyClass & MyClass::operator=(class MyClass const &)
int MyClass::Bar
class MyOtherClass MyClass::Foo
int MyClass::OtherStuff

显然已导出变量MyClass::Foo,但类MyOtherClass 。如果您尝试从该静态变量访问MyOtherClass::something,我不确定在这种情况下会发生什么。