Sign in to follow this  
BolekPL

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 this post


Link to post
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)
#endif

class 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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this