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. Can I not use static const when declaring members?

Can I not use static const when declaring members?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 5 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.
  • T Offline
    T Offline
    tom76
    wrote on last edited by
    #1

    Can I just check, is this code valid? The compiler claims there's a syntax error - 'constant' - for each member. It claims the same even when they are just regular ints too! class MyClass { public: MyClass(){}; ~MyClass(){}; public: static const int MEMBER_ONE; static const int MEMBER_TWO; //... etc. }; const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; and static const MyClass MyObj; // One instance, declared in header I should note that I'm #including this file into another header, so that class can have a reference to MyObj as a member. Obseve everything, remember more...

    N J 2 Replies Last reply
    0
    • T tom76

      Can I just check, is this code valid? The compiler claims there's a syntax error - 'constant' - for each member. It claims the same even when they are just regular ints too! class MyClass { public: MyClass(){}; ~MyClass(){}; public: static const int MEMBER_ONE; static const int MEMBER_TWO; //... etc. }; const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; and static const MyClass MyObj; // One instance, declared in header I should note that I'm #including this file into another header, so that class can have a reference to MyObj as a member. Obseve everything, remember more...

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      tom76 wrote: const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; These must appear only once, so move them from the .h to the .cpp that defines the class. It would also help a lot if you included the specific compiler error and indicated what line it referred to. tom76 wrote: static const MyClass MyObj; This instantiating an instance of the class. Is that what you realy want. I don't know if you can have a const class like this. Also globals are bad, very bad. Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program

      T 1 Reply Last reply
      0
      • N Neville Franks

        tom76 wrote: const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; These must appear only once, so move them from the .h to the .cpp that defines the class. It would also help a lot if you included the specific compiler error and indicated what line it referred to. tom76 wrote: static const MyClass MyObj; This instantiating an instance of the class. Is that what you realy want. I don't know if you can have a const class like this. Also globals are bad, very bad. Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program

        T Offline
        T Offline
        tom76
        wrote on last edited by
        #3

        I've not got a definition (.cpp file) for my class, as all I am using it for is to create a static const object with these members so everything is in MyClass.h . I believe I need a global because I only want one instance of this object, because the members will never change value. The errors are error C2059: syntax error : 'constant' error C2238: unexpected token(s) preceding ';' for each member. Obseve everything, remember more...

        J 1 Reply Last reply
        0
        • T tom76

          I've not got a definition (.cpp file) for my class, as all I am using it for is to create a static const object with these members so everything is in MyClass.h . I believe I need a global because I only want one instance of this object, because the members will never change value. The errors are error C2059: syntax error : 'constant' error C2238: unexpected token(s) preceding ';' for each member. Obseve everything, remember more...

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          tom76 wrote: I believe I need a global because I only want one instance of this object, because the members will never change value. Would it be better to make your variables (which seem to be constants) constant members of the Application-class? So you can access them (almost) everywhere in your program and yet they are not global. Example:

          Yourapp.h

          class CYourapp : CApp
          {
          [...]
          public:
          const int m_One;
          const int m_Two;
          [...]
          }

          Yourapp.cpp

          [...]
          CYourapp::m_One = 1;
          CYourapp::m_Two = 2;
          [...]

          Use in either of this ways:

          int i = theApp.m_One;
          int j = static_cast(AfxGetApp())->m_Two;


          My opinions may have changed, but not the fact that I am right.

          T 1 Reply Last reply
          0
          • J jhwurmbach

            tom76 wrote: I believe I need a global because I only want one instance of this object, because the members will never change value. Would it be better to make your variables (which seem to be constants) constant members of the Application-class? So you can access them (almost) everywhere in your program and yet they are not global. Example:

            Yourapp.h

            class CYourapp : CApp
            {
            [...]
            public:
            const int m_One;
            const int m_Two;
            [...]
            }

            Yourapp.cpp

            [...]
            CYourapp::m_One = 1;
            CYourapp::m_Two = 2;
            [...]

            Use in either of this ways:

            int i = theApp.m_One;
            int j = static_cast(AfxGetApp())->m_Two;


            My opinions may have changed, but not the fact that I am right.

            T Offline
            T Offline
            tom76
            wrote on last edited by
            #5

            Thanks, I'll have a go at that I think. Still don't know why I am getting these syntax errors though. Oh well. Obseve everything, remember more...

            C 1 Reply Last reply
            0
            • T tom76

              Thanks, I'll have a go at that I think. Still don't know why I am getting these syntax errors though. Oh well. Obseve everything, remember more...

              C Offline
              C Offline
              catchnine
              wrote on last edited by
              #6

              At first sight, It's a little strange. 'Cause my poor skill :confused: Why const variable have to be a static one? My wild guess is that a const variable will also have just a one instance between several class instance like a static. Actually, It's not exactly right. But I think const and static have similitiy on each other in this case. Develope yourself

              1 Reply Last reply
              0
              • T tom76

                Can I just check, is this code valid? The compiler claims there's a syntax error - 'constant' - for each member. It claims the same even when they are just regular ints too! class MyClass { public: MyClass(){}; ~MyClass(){}; public: static const int MEMBER_ONE; static const int MEMBER_TWO; //... etc. }; const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; and static const MyClass MyObj; // One instance, declared in header I should note that I'm #including this file into another header, so that class can have a reference to MyObj as a member. Obseve everything, remember more...

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                class MyClass { public: MyClass() : MEMBER_ONE(64), MEMBER_TWO(128){}; ~MyClass(){}; public: const int MEMBER_ONE; const int MEMBER_TWO; //... etc. }; Trust in the code Luke. Yea right!

                T 1 Reply Last reply
                0
                • J John R Shaw

                  class MyClass { public: MyClass() : MEMBER_ONE(64), MEMBER_TWO(128){}; ~MyClass(){}; public: const int MEMBER_ONE; const int MEMBER_TWO; //... etc. }; Trust in the code Luke. Yea right!

                  T Offline
                  T Offline
                  tom76
                  wrote on last edited by
                  #8

                  I'll try it, thanks. Obseve everything, remember more...

                  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