# Static variables in classes

## Recommended Posts

Hello I have question to You. Suppose we have a class: class CClass { public: static int st_iBlah; }; Now we have header with this class in our application and in dynamic library. Somewhere we load dll in application. Now my question is: How to connect this classes(? one class but in different "places") to share only one instance of st_iBlah between application and dll ? Other exapmle: There is static library "core" which contain singleton CCore and we link it to application and to dynamic library. Again somewhere in application we load dll and i want use in this dll the same CCore object that is used in application. How to do it ?

##### Share on other sites
You need to used the __declspec attribute, assuming you're using MS compiler. However, the argument to the attribute depends on the way it's used, so, in the header have:
#if defined COMPILING_DLL_WITH_CLASS#define CLASS_ATTRIBUTE __declspec (dllexport)#else#define CLASS_ATTRIBUTE __declspec (dllimport)#endifclass CLASS_ATTRIBUTE CClass{  // stuff here as before};

When you compile the DLL with the class defined in it, define COMPILING_DLL_WITH_CLASS. For any clients of the class, don't define that symbol.

Skizz

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978697

• 21
• 14
• 12
• 10
• 12