Search code examples
c++visual-c++dlldllimportdllexport

Accessibility of static data members in an exported class (DLL)?


Suppose I have this class:

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

I have some questions (I'm using an MSVC compiler):

  1. Will the static member "Bar" be accessible to clients that import this class?
  2. Will the static member "OtherStuff" also be exported? If not, is this due to the access modifier, private:?
  3. If the class MyOtherClass is not defined with __declspec(dllexport), I believe this means warning C4251 will be issued by the MSVC compiler, but does this mean that variable Foo will not be accessible to clients that import this class?

I'm basically just running various scenarios through my mind, trying to figure out what is and what isn't exported (and thus inaccessible) in a DLL class interface in terms of static data members only.


Solution

  • For the code:

    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;
    

    I get the following in Dependency Walker:

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

    Apparently variable MyClass::Foo is indeed exported, but class MyOtherClass is not. I'm not sure what happens in this case if you try to access MyOtherClass::something from that static variable.