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. static const foo = 1 works ?!

static const foo = 1 works ?!

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
13 Posts 6 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.
  • M Mr Brainley

    I have the following code :

    class Test
    {
    public:
    static const foo = 1;
    static const bar = 2;
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    cout << Test::foo << endl << Test::bar << endl;

    return 0;
    

    }

    - and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley

    W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #3

    Mr.Brainley wrote:

    and realized later that it shouldn't work

    Why shouldn't it work? It's static, meaning all instances share the same varialble, and it's const meaning it will never change. So it's perfectly safe to declare it as you have done.

    B 1 Reply Last reply
    0
    • J James R Twine

      If you mean why does this get treated like a const int, that is because that is exactly what you created when you typed const foo.    I believe that int is treated as the default type in a scenaro like this, which is why you sometimes see code like unsigned uiABC = 0 to create an unsigned **int** variable.    Peace!

      -=- James
      Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
      See DeleteFXPFiles

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #4

      I wonder if it would be treated as a QWORD on a 64-bit machine :confused:

      J R 2 Replies Last reply
      0
      • J James R Twine

        If you mean why does this get treated like a const int, that is because that is exactly what you created when you typed const foo.    I believe that int is treated as the default type in a scenaro like this, which is why you sometimes see code like unsigned uiABC = 0 to create an unsigned **int** variable.    Peace!

        -=- James
        Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        See DeleteFXPFiles

        M Offline
        M Offline
        Mr Brainley
        wrote on last edited by
        #5

        Is that MS-Specific or ISO ? wbr Brainley

        J 1 Reply Last reply
        0
        • W Waldermort

          I wonder if it would be treated as a QWORD on a 64-bit machine :confused:

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #6

          Not if they keep the width of the int type at 32-bits...  But I do not have a 64-bit environment handy...    Peace!

          -=- James
          Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          See DeleteFXPFiles

          1 Reply Last reply
          0
          • M Mr Brainley

            I have the following code :

            class Test
            {
            public:
            static const foo = 1;
            static const bar = 2;
            };

            int _tmain(int argc, _TCHAR* argv[])
            {
            cout << Test::foo << endl << Test::bar << endl;

            return 0;
            

            }

            - and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley

            B Offline
            B Offline
            benjymous
            wrote on last edited by
            #7

            Yeah, in versions of VS prior to V2005, it will automatically assume you meant static const int foo = 1; In VS2005, it'll give a sensible error (We found exactly this in some of our code when upgrading projects to 2005)

            1 Reply Last reply
            0
            • M Mr Brainley

              Is that MS-Specific or ISO ? wbr Brainley

              J Offline
              J Offline
              James R Twine
              wrote on last edited by
              #8

              I think that is standard, or at least assumed.  I have seen bits of older code, on older VAXen or U*ix boxes, that has things like:

              static SomeVariable = 1;
              unsigned SomeOtherValue = 0;

              Peace!

              -=- James
              Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              See DeleteFXPFiles

              1 Reply Last reply
              0
              • W Waldermort

                Mr.Brainley wrote:

                and realized later that it shouldn't work

                Why shouldn't it work? It's static, meaning all instances share the same varialble, and it's const meaning it will never change. So it's perfectly safe to declare it as you have done.

                B Offline
                B Offline
                benjymous
                wrote on last edited by
                #9

                WalderMort wrote:

                Why shouldn't it work?

                No data type.

                1 Reply Last reply
                0
                • W Waldermort

                  I wonder if it would be treated as a QWORD on a 64-bit machine :confused:

                  R Offline
                  R Offline
                  Roger Stoltz
                  wrote on last edited by
                  #10

                  WalderMort wrote:

                  I wonder if it would be treated as a QWORD on a 64-bit machin

                  Probably. int data type is environment specific which means that it's 16 bits wide in a compiler for 16-bit systems, e.g. the C-compiler for MicroChip PIC 18[^]. This is the standard, but a complier might not follow the standard.


                  "It's supposed to be hard, otherwise anybody could do it!" - selfquote

                  W 1 Reply Last reply
                  0
                  • R Roger Stoltz

                    WalderMort wrote:

                    I wonder if it would be treated as a QWORD on a 64-bit machin

                    Probably. int data type is environment specific which means that it's 16 bits wide in a compiler for 16-bit systems, e.g. the C-compiler for MicroChip PIC 18[^]. This is the standard, but a complier might not follow the standard.


                    "It's supposed to be hard, otherwise anybody could do it!" - selfquote

                    W Offline
                    W Offline
                    Waldermort
                    wrote on last edited by
                    #11

                    but the AMD 64-bit[^] docs specify that an int remains 32-bits in size. So I guess it really depends on how the compiler decides to interpret the code.

                    R 1 Reply Last reply
                    0
                    • W Waldermort

                      but the AMD 64-bit[^] docs specify that an int remains 32-bits in size. So I guess it really depends on how the compiler decides to interpret the code.

                      R Offline
                      R Offline
                      Roger Stoltz
                      wrote on last edited by
                      #12

                      WalderMort wrote:

                      but the AMD 64-bit[^] docs specify that an int remains 32-bits in size. So I guess it really depends on how the compiler decides to interpret the code.

                      Yep, very true.


                      "It's supposed to be hard, otherwise anybody could do it!" - selfquote

                      1 Reply Last reply
                      0
                      • M Mr Brainley

                        I have the following code :

                        class Test
                        {
                        public:
                        static const foo = 1;
                        static const bar = 2;
                        };

                        int _tmain(int argc, _TCHAR* argv[])
                        {
                        cout << Test::foo << endl << Test::bar << endl;

                        return 0;
                        

                        }

                        - and absolutely no idea why it is working. I left the type declaration by accident and realized later that it shouldn't work - at least in my book. But it does. Can anyone explain that to me ? Is there some kind of default data-type for such situations ? If so, what is it ? wbr Brainley

                        M Offline
                        M Offline
                        Michael Dunn
                        wrote on last edited by
                        #13

                        As others have said, the compiler assumes you mean int if you don't specify a type. This was a holdover from C and is illegal in newer versions of the C++ spec.

                        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                        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