2012-02-16 55 views
1

假設我有這個類:導出類(DLL)中靜態數據成員的可訪問性?

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

我有一些問題(我使用的是MSVC編譯器):

  1. 將靜態成員「酒吧」是那些進口此類客戶端訪問?
  2. 靜態成員「OtherStuff」是否也會被導出?如果不是,這是由於訪問修飾符private:
  3. 如果該類MyOtherClass不與__declspec(dllexport)定義,我認爲這意味着警告C4251將由MSVC編譯器發出的,但是這是否意味着變量Foo不會給進口該類客戶機訪問?

我基本上只是在腦海中運行各種場景,試圖找出什麼是什麼,什麼是不導出(因此無法訪問)在DLL類接口靜態數據成員方面。

+0

你可能想要構造一個測試DLL,然後運行dumpbin/exports - 這會告訴你一定發生了什麼。 – Bukes 2012-02-16 22:33:07

+0

@Bukes是否有某種方式可以使用Dependency Walker來檢查? – 2012-02-16 22:40:49

+0

Dependency Walker當前版本中的導出窗格將向您顯示從DLL導出的所有內容,包括變量和類。您也可以「取消裝飾符號」,從而刪除一些名稱的混亂。 – Bukes 2012-02-16 23:09:33

回答

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