0

I declare then instantiate a static variable in a DLL.

// DLL.h
class A
{
    //...
};

static A* a;



// DLL.cpp
A* a = new A;

So far, so good... I was suggested to use extern rather than static.

extern A* a; // in DLL.h

No problem with that but the extern variable must be declared somewhere. I got Invalid storage class member.

In other words, what I was used to do is to declare a variable in a source file like this:

// In src.cpp
A a; 

then extern declare it in another source file in the same project:

// In src2.cpp
extern A a;

so it is the same object a at link time. Maybe it is not the right thing to do?

So, where to declare the variable that is now extern?

Note that I used static declaration in order to see the variable instantiated as soon as the dll is loaded.

Note that the current use of static works most of the time but I think I observe a delay or something like this in the variable instantiation while it should always be instantiated at load time. I'm investigating this problem for a week now and I can't find no solution.

dom_beau
  • 2,437
  • 3
  • 30
  • 59
  • Either way this is a horrible and unsafe way to do singletons. Use a private static variable and a function to access it. See e.g. http://stackoverflow.com/questions/1008019/c-singleton-design-pattern (And I've never seen sane usage of `extern` in C++) – Frank Osterfeld Nov 01 '13 at 18:15
  • Well, actually, it is *realy* not the way I do singletons... But We had to code that way for a particular purpose. Anyway, I thing my problem won't be solved with such questions. It is probably much more complex than what I can explain here. Thanks anyway. – dom_beau Nov 04 '13 at 14:08

0 Answers0