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. const in class as a member variable?

const in class as a member variable?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 3 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
    sam_psycho
    wrote on last edited by
    #1

    Is it possible to declare member variable of class as a const, if yes,please give me 1 example how to initialize it and if not, why?

    C CPalliniC 2 Replies Last reply
    0
    • S sam_psycho

      Is it possible to declare member variable of class as a const, if yes,please give me 1 example how to initialize it and if not, why?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Yes, it is possible. You have to initialize it in the constructor initialization list:

      MyClass::MyClass() : myConstInt(5)
      {
      }

      You can also initialize it in the header file too, when declaring it:

      class MyClass
      {
      const int myConstInt = 5;
      }

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      S 1 Reply Last reply
      0
      • S sam_psycho

        Is it possible to declare member variable of class as a const, if yes,please give me 1 example how to initialize it and if not, why?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        sam_psycho wrote:

        Is it possible to declare member variable of class as a const,

        Yes.

        sam_psycho wrote:

        if yes,please give me 1 example how to initialize it

        class A
        {
        static const int c = 299792458;
        const int a;

        A():a(5){}
        };

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        S 1 Reply Last reply
        0
        • C Cedric Moonen

          Yes, it is possible. You have to initialize it in the constructor initialization list:

          MyClass::MyClass() : myConstInt(5)
          {
          }

          You can also initialize it in the header file too, when declaring it:

          class MyClass
          {
          const int myConstInt = 5;
          }

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          S Offline
          S Offline
          sam_psycho
          wrote on last edited by
          #4

          This is working fine

          MyClass::MyClass() : myConstInt(5)
          {
          }

          but it is not possible,

          class MyClass
          {
          const int myConstInt = 5;
          }

          compiler is giving error, So I guess only one way to initialize const in a class..

          C 1 Reply Last reply
          0
          • S sam_psycho

            This is working fine

            MyClass::MyClass() : myConstInt(5)
            {
            }

            but it is not possible,

            class MyClass
            {
            const int myConstInt = 5;
            }

            compiler is giving error, So I guess only one way to initialize const in a class..

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Ooops, my bad. I don't use that notation very often. I thought it was ok but it is only valid if the member is static also.

            Cédric Moonen Software developer
            Charting control [v2.0] OpenGL game tutorial in C++

            1 Reply Last reply
            0
            • CPalliniC CPallini

              sam_psycho wrote:

              Is it possible to declare member variable of class as a const,

              Yes.

              sam_psycho wrote:

              if yes,please give me 1 example how to initialize it

              class A
              {
              static const int c = 299792458;
              const int a;

              A():a(5){}
              };

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              S Offline
              S Offline
              sam_psycho
              wrote on last edited by
              #6

              from above discussion I have come to conclusion that compiler does not allow any type of initialization in class, so statement

              static const int c = 299792458;

              in your code also creating problem... :(

              CPalliniC 1 Reply Last reply
              0
              • S sam_psycho

                from above discussion I have come to conclusion that compiler does not allow any type of initialization in class, so statement

                static const int c = 299792458;

                in your code also creating problem... :(

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                You're wrong (I'm right, of course... :rolleyes: ). It looks like you missed the static modifier. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                S 1 Reply Last reply
                0
                • CPalliniC CPallini

                  You're wrong (I'm right, of course... :rolleyes: ). It looks like you missed the static modifier. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  S Offline
                  S Offline
                  sam_psycho
                  wrote on last edited by
                  #8

                  who is lying compiler or you?..but I should believe in compiler and it says that error: c:\test.cpp 6: cannot initialize class member here try this code

                  class temp
                  {
                  static const int i=10;
                  };
                  void main()
                  {
                  }

                  CPalliniC 1 Reply Last reply
                  0
                  • S sam_psycho

                    who is lying compiler or you?..but I should believe in compiler and it says that error: c:\test.cpp 6: cannot initialize class member here try this code

                    class temp
                    {
                    static const int i=10;
                    };
                    void main()
                    {
                    }

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    I never lie. Anyway maybe your compiler follows strictly the C++ stanbdard. (my compiler doesn't complain, see [^]). :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    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