Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. calling a static member function

calling a static member function

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steph5
    wrote on last edited by
    #1

    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:

    M 1 Reply Last reply
    0
    • S steph5

      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:

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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)
      #endif

      Then 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

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        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)
        #endif

        Then 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

        S Offline
        S Offline
        steph5
        wrote on last edited by
        #3

        Thanks for the comprehensive answer Mark. Really appreciate it :)

        M 1 Reply Last reply
        0
        • S steph5

          Thanks for the comprehensive answer Mark. Really appreciate it :)

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups