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. c++ and using public static members in a class

c++ and using public static members in a class

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
8 Posts 4 Posters 2 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.
  • A Offline
    A Offline
    Alan Kurlansky
    wrote on last edited by
    #1

    How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.

    C A A 4 Replies Last reply
    0
    • A Alan Kurlansky

      How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      this worked for me:

      class A
      {
      public:
      static void g(int)
      {
      int i = 0;
      }
      };

      typedef void (*fp)(int);

      int _tmain(int argc, _TCHAR* argv[])
      {
      fp p = A::g;
      p(1);
      return 0;
      }

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • A Alan Kurlansky

        How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.

        A Offline
        A Offline
        Alain Rist
        wrote on last edited by
        #3

        // MyClass.h
        class MyClass
        {
        static INT m_i;
        // ...
        };
        // if you want to keep it in definition part
        __declspec(selectany) INT MyClass::m_i; // default initialized to 0

        or

        // MyClass.cpp
        // ...
        // if you want to have it in implementation part
        INT MyClass::m_i; // default initialized to 0
        // ...

        cheers, AR

        When the wise person points at the moon the fool looks at the finger (Chinese proverb)

        1 Reply Last reply
        0
        • A Alan Kurlansky

          How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.

          A Offline
          A Offline
          Avi Berger
          wrote on last edited by
          #4

          Static member functions usage: MyClass.h:

          class MyClass
          {
          public:
          static int fcn( int param );
          static int inlineFcn( int param) ) // may be inlined, but not guaranteed
          {
          // just a little stuff
          };
          };

          MyClass.cpp:

          #include "Myclass.h"

          int MyClass::fcn( int param )
          {
          // stuff
          }

          Client.cpp:

          #include "Myclass.h"

          void clientFcn( int a, int b )
          {
          // stuff

          int c = MyClass::fcn( a );
          int d = MyClass::inlineFcn( b );
          
          // more stuff
          

          }

          It sounds to me that another approach might be more suitable for your purposes. I'll post that next.

          Please do not read this signature.

          1 Reply Last reply
          0
          • A Alan Kurlansky

            How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.

            A Offline
            A Offline
            Avi Berger
            wrote on last edited by
            #5

            Alan Kurlansky wrote:

            a way to help organize the code a bit better

            It sounds to me like you want the organization, but what you are doing is not truly about classes and object orientation. There is another facility in C++, namespaces, that may be more appropriate for your current use. It organizes things in groups and helps avoid naming conflicts, but does not raise the "object" expectations of someone reading your code. In some ways using namespaces looks a lot like what I posted for static member functions. This is how you might use namespaces: Group.h:

            namespace Group
            {
            int fcn( int param );
            inline int inlineFcn( int param) ) // may be inlined, but not guaranteed
            {
            // just a little stuff
            };
            }

            Group.cpp:

            #include "Group.h"

            namespace Group
            {
            int fcn( int param )
            {
            // stuff
            }
            }

            Client.cpp:

            #include "Group.h"

            void clientFcn( int a, int b )
            {
            // stuff

            int c = Group::fcn( a );
            int d = Group::inlineFcn( b );
            
            // more stuff
            

            }

            AlternateClient.cpp:

            #include "Group.h"

            using namespace Group;

            void altClientFcn( int a, int b )
            {
            // stuff

            int c = fcn( a );
            int d = inlineFcn( b );
            
            // more stuff
            

            }

            Please do not read this signature.

            A 2 Replies Last reply
            0
            • A Avi Berger

              Alan Kurlansky wrote:

              a way to help organize the code a bit better

              It sounds to me like you want the organization, but what you are doing is not truly about classes and object orientation. There is another facility in C++, namespaces, that may be more appropriate for your current use. It organizes things in groups and helps avoid naming conflicts, but does not raise the "object" expectations of someone reading your code. In some ways using namespaces looks a lot like what I posted for static member functions. This is how you might use namespaces: Group.h:

              namespace Group
              {
              int fcn( int param );
              inline int inlineFcn( int param) ) // may be inlined, but not guaranteed
              {
              // just a little stuff
              };
              }

              Group.cpp:

              #include "Group.h"

              namespace Group
              {
              int fcn( int param )
              {
              // stuff
              }
              }

              Client.cpp:

              #include "Group.h"

              void clientFcn( int a, int b )
              {
              // stuff

              int c = Group::fcn( a );
              int d = Group::inlineFcn( b );
              
              // more stuff
              

              }

              AlternateClient.cpp:

              #include "Group.h"

              using namespace Group;

              void altClientFcn( int a, int b )
              {
              // stuff

              int c = fcn( a );
              int d = inlineFcn( b );
              
              // more stuff
              

              }

              Please do not read this signature.

              A Offline
              A Offline
              Alan Kurlansky
              wrote on last edited by
              #6

              Yes, organization is what I'm looking to do in this case. This looks good. I'll give this a try. Thanks

              A 1 Reply Last reply
              0
              • A Avi Berger

                Alan Kurlansky wrote:

                a way to help organize the code a bit better

                It sounds to me like you want the organization, but what you are doing is not truly about classes and object orientation. There is another facility in C++, namespaces, that may be more appropriate for your current use. It organizes things in groups and helps avoid naming conflicts, but does not raise the "object" expectations of someone reading your code. In some ways using namespaces looks a lot like what I posted for static member functions. This is how you might use namespaces: Group.h:

                namespace Group
                {
                int fcn( int param );
                inline int inlineFcn( int param) ) // may be inlined, but not guaranteed
                {
                // just a little stuff
                };
                }

                Group.cpp:

                #include "Group.h"

                namespace Group
                {
                int fcn( int param )
                {
                // stuff
                }
                }

                Client.cpp:

                #include "Group.h"

                void clientFcn( int a, int b )
                {
                // stuff

                int c = Group::fcn( a );
                int d = Group::inlineFcn( b );
                
                // more stuff
                

                }

                AlternateClient.cpp:

                #include "Group.h"

                using namespace Group;

                void altClientFcn( int a, int b )
                {
                // stuff

                int c = fcn( a );
                int d = inlineFcn( b );
                
                // more stuff
                

                }

                Please do not read this signature.

                A Offline
                A Offline
                Alan Kurlansky
                wrote on last edited by
                #7

                Thanks. This looks good. I'll give it a try.

                1 Reply Last reply
                0
                • A Alan Kurlansky

                  Yes, organization is what I'm looking to do in this case. This looks good. I'll give this a try. Thanks

                  A Offline
                  A Offline
                  Avi Berger
                  wrote on last edited by
                  #8

                  Good luck. I hope it works well for you. I will add a note about style. My example showed indenting a level inside the namespace definitions, just like you would do for any other pair of {}'s. There are style guidelines that recommend not doing so for namespaces. I'm undecided about this myself.

                  Please do not read this signature.

                  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