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. simple question about "static"

simple question about "static"

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelpquestion
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 Offline
    M Offline
    Maer727
    wrote on last edited by
    #1

    - I use a static data member of a class. But the following codes have an error and a warning, "C:\TestConst\TestConst.cpp(8) : error C2057: expected constant expression C:\TestConst\TestConst.cpp(8) : warning C4200: nonstandard extension used : zero-sized array in struct/union" - I do not know why. (Such are my codes.) #include "iostream.h" class a{ private: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<

    X N L 3 Replies Last reply
    0
    • M Maer727

      - I use a static data member of a class. But the following codes have an error and a warning, "C:\TestConst\TestConst.cpp(8) : error C2057: expected constant expression C:\TestConst\TestConst.cpp(8) : warning C4200: nonstandard extension used : zero-sized array in struct/union" - I do not know why. (Such are my codes.) #include "iostream.h" class a{ private: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<

      X Offline
      X Offline
      Xavier Shay
      wrote on last edited by
      #2

      Maybe the compiler is trying to allocate memory for i[s] before it reachs the const a::s=20; line, causing an error because s would equal 0. Hope this helps - X

      M 1 Reply Last reply
      0
      • M Maer727

        - I use a static data member of a class. But the following codes have an error and a warning, "C:\TestConst\TestConst.cpp(8) : error C2057: expected constant expression C:\TestConst\TestConst.cpp(8) : warning C4200: nonstandard extension used : zero-sized array in struct/union" - I do not know why. (Such are my codes.) #include "iostream.h" class a{ private: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        Hello I tried the following code using GNU C++ compiler on Linux [Debian 2.2] It compiled and ran fine #include "iostream.h" class a{ public: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<

        M 1 Reply Last reply
        0
        • M Maer727

          - I use a static data member of a class. But the following codes have an error and a warning, "C:\TestConst\TestConst.cpp(8) : error C2057: expected constant expression C:\TestConst\TestConst.cpp(8) : warning C4200: nonstandard extension used : zero-sized array in struct/union" - I do not know why. (Such are my codes.) #include "iostream.h" class a{ private: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You have used integer array as an data member in the class.Memory is allocated to the array at compile time itself.So arrays always want constant expression.So it gives an error. Hope this helps....:)

          S M M 3 Replies Last reply
          0
          • L Lost User

            You have used integer array as an data member in the class.Memory is allocated to the array at compile time itself.So arrays always want constant expression.So it gives an error. Hope this helps....:)

            S Offline
            S Offline
            Sprudling
            wrote on last edited by
            #5

            Since the variable is a constant the compiler knows the value at compile time. Therefore it's ok to use a const variable to declare a non-dynamic array. Anyway, here is what you can do...

            #include "iostream.h"

            class ClassA
            {
            private:
            const static enum { s = 20 };
            int i[s];

            public:
            int Func(int n);
            static int StaticFunc(int n);

            };

            int ClassA::Func(int n)
            {
            return ClassA::s + n;
            }

            int ClassA::StaticFunc(int n)
            {
            return ClassA::s + n;
            }

            void main()
            {
            cout << sizeof(int) << endl;
            cout << sizeof(ClassA) << endl;

            cout << ClassA::StaticFunc(1) << endl;
            ClassA a;
            cout << a.Func(2) << endl;
            

            }

            It's not necessary to specify ClassA:: before the variable name (s) in the class member functions, but I like it this way :) Sprudling

            M 1 Reply Last reply
            0
            • L Lost User

              You have used integer array as an data member in the class.Memory is allocated to the array at compile time itself.So arrays always want constant expression.So it gives an error. Hope this helps....:)

              M Offline
              M Offline
              Malcolm McMahon
              wrote on last edited by
              #6

              Actually memory for the array isn't allocated at all since the class is never instanciated. But the forward reference for the value of s does make it impossible for the compiler to lay out the class structure fully until the reference is satisfied. It's just being a lazy compiler.

              M 1 Reply Last reply
              0
              • N Nish Nishant

                Hello I tried the following code using GNU C++ compiler on Linux [Debian 2.2] It compiled and ran fine #include "iostream.h" class a{ public: static const int s ; int i[s]; }; const int a::s=20; void main() { cout<

                M Offline
                M Offline
                Maer727
                wrote on last edited by
                #7

                - Thanks pal! - Have you tried with VC++ 6.0? It do have problems. - Can you help? - Regards, Maer

                1 Reply Last reply
                0
                • L Lost User

                  You have used integer array as an data member in the class.Memory is allocated to the array at compile time itself.So arrays always want constant expression.So it gives an error. Hope this helps....:)

                  M Offline
                  M Offline
                  Maer727
                  wrote on last edited by
                  #8

                  - Thanks pal! - But I think the error occurs with the statement "const int a:: s=20; " - Do you agree with me? - Can you help? - Regards, Maer

                  1 Reply Last reply
                  0
                  • S Sprudling

                    Since the variable is a constant the compiler knows the value at compile time. Therefore it's ok to use a const variable to declare a non-dynamic array. Anyway, here is what you can do...

                    #include "iostream.h"

                    class ClassA
                    {
                    private:
                    const static enum { s = 20 };
                    int i[s];

                    public:
                    int Func(int n);
                    static int StaticFunc(int n);

                    };

                    int ClassA::Func(int n)
                    {
                    return ClassA::s + n;
                    }

                    int ClassA::StaticFunc(int n)
                    {
                    return ClassA::s + n;
                    }

                    void main()
                    {
                    cout << sizeof(int) << endl;
                    cout << sizeof(ClassA) << endl;

                    cout << ClassA::StaticFunc(1) << endl;
                    ClassA a;
                    cout << a.Func(2) << endl;
                    

                    }

                    It's not necessary to specify ClassA:: before the variable name (s) in the class member functions, but I like it this way :) Sprudling

                    M Offline
                    M Offline
                    Maer727
                    wrote on last edited by
                    #9

                    - Thanks pal! - Your reply helps a lot. I still have a question. What means "variable is a constant the compiler knows the value at compile time" in your reply? - Can you show me an example? - Regards, Maer

                    1 Reply Last reply
                    0
                    • M Malcolm McMahon

                      Actually memory for the array isn't allocated at all since the class is never instanciated. But the forward reference for the value of s does make it impossible for the compiler to lay out the class structure fully until the reference is satisfied. It's just being a lazy compiler.

                      M Offline
                      M Offline
                      Maer727
                      wrote on last edited by
                      #10

                      - Thanks pal! - Your reply helps a lot. I still have a question. Another pal says, "the variable is a constant the compiler knows the value at compile time". I do not know what is meaning. - Can you help? - Regards, Maer

                      1 Reply Last reply
                      0
                      • X Xavier Shay

                        Maybe the compiler is trying to allocate memory for i[s] before it reachs the const a::s=20; line, causing an error because s would equal 0. Hope this helps - X

                        M Offline
                        M Offline
                        Maer727
                        wrote on last edited by
                        #11

                        - Thanks pal! - Your reply helps a lot. I still have a question. Another pal says, "the variable is a constant the compiler knows the value at compile time". I do not know what is meaning. - Can you help? - Regards, Maer

                        X 1 Reply Last reply
                        0
                        • M Maer727

                          - Thanks pal! - Your reply helps a lot. I still have a question. Another pal says, "the variable is a constant the compiler knows the value at compile time". I do not know what is meaning. - Can you help? - Regards, Maer

                          X Offline
                          X Offline
                          Xavier Shay
                          wrote on last edited by
                          #12

                          I know constants in VB are known at compile time, but these are equivalent to #define's in VC. The const keyword means that the variable cannot be altered during the execution of the program. - X

                          M 1 Reply Last reply
                          0
                          • X Xavier Shay

                            I know constants in VB are known at compile time, but these are equivalent to #define's in VC. The const keyword means that the variable cannot be altered during the execution of the program. - X

                            M Offline
                            M Offline
                            Maer727
                            wrote on last edited by
                            #13

                            - Thanks pal! - Your reply helps a lot. I still have a question. - Can "const" and "static" use together? (Like, const static int s=10; ) - Can you help? - Have a nice weekend, Maer

                            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