calling a static member function
-
Hi Guys I am working with Visual Stusios 2005. I have a solution with 2 projects in it. Can I call a static member function that is declared in one project from the other project? OtherProjectClass::StaticMemberFunction(variables); At the moment I get error LNK2019 Thanks:confused:
-
Hi Guys I am working with Visual Stusios 2005. I have a solution with 2 projects in it. Can I call a static member function that is declared in one project from the other project? OtherProjectClass::StaticMemberFunction(variables); At the moment I get error LNK2019 Thanks:confused:
You can export the class from the project that implements the class:
class __declspec(dllexport) test
{
public:
static void StaticMemberFunction();
};void test::StaticMemberFunction()
{
// do something
}Then on the consumer side, import the class:
class __declspec(dllimport) test
{
public:
static void StaticMemberFunction();
};
...
// call the imported member function
test::StaticMemberFunction();Note that it's nicer to use a macro that expands to __declspec(dllexport) or __declspec(dllimport) depending on the definition of a build-type macro:
// define BUILDINGDLL in the DLL project compiler preprocessor options
#if defined(BUILDINGDLL)
#define MYIMPORTEXPORT __declspec(dllexport)
#else
#define MYIMPORTEXPORT __declspec(dllimport)
#endifThen both sides can share the same header file for the class, making maintainability easier (if the class changes you don't have to remember to change it in two places):
class MYIMPORTEXPORT test
{
public:
static void StaticMemberFunction();
};*EDIT* I forgot to mention - you'll need to add the import library for the implementing module to the importing project's linker input settings.
Mark Salsbery Microsoft MVP - Visual C++ :java:
modified on Saturday, September 20, 2008 4:33 PM
-
You can export the class from the project that implements the class:
class __declspec(dllexport) test
{
public:
static void StaticMemberFunction();
};void test::StaticMemberFunction()
{
// do something
}Then on the consumer side, import the class:
class __declspec(dllimport) test
{
public:
static void StaticMemberFunction();
};
...
// call the imported member function
test::StaticMemberFunction();Note that it's nicer to use a macro that expands to __declspec(dllexport) or __declspec(dllimport) depending on the definition of a build-type macro:
// define BUILDINGDLL in the DLL project compiler preprocessor options
#if defined(BUILDINGDLL)
#define MYIMPORTEXPORT __declspec(dllexport)
#else
#define MYIMPORTEXPORT __declspec(dllimport)
#endifThen both sides can share the same header file for the class, making maintainability easier (if the class changes you don't have to remember to change it in two places):
class MYIMPORTEXPORT test
{
public:
static void StaticMemberFunction();
};*EDIT* I forgot to mention - you'll need to add the import library for the implementing module to the importing project's linker input settings.
Mark Salsbery Microsoft MVP - Visual C++ :java:
modified on Saturday, September 20, 2008 4:33 PM
-
If __declspec(dllexport) works for your needs, you can also use it on individual class functions witout exporting the entire class... Exporting from a DLL Using __declspec(dllexport)[^] Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: