Constant member of a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anarki

    Constant member of a class

    #include <iostream>
    using namespace std;

    class ConstantMember
    {
    const int m_const;
    public:
    ConstantMember( int cons = 10):m_const(con s){}
    int getConst() const{ return m_const;}
    };

    int main()
    {
    ConstantMember X;//(20);
    cout << X.getConst();
    return 0;
    }

    the above is a program that was compiled & linked using g++. Is there
    any other way to initialise a constant member variable using
    constructor while creating objects?
  • utab

    #2
    Re: Constant member of a class

    On Fri, 04 Jul 2008 02:08:13 -0700, Anarki wrote:
    #include <iostream>
    using namespace std;
    >
    class ConstantMember
    {
    const int m_const;
    public:
    ConstantMember( int cons = 10):m_const(con s){} int getConst() const
    {
    return m_const;}
    };
    >
    int main()
    {
    ConstantMember X;//(20);
    cout << X.getConst();
    return 0;
    }
    >
    the above is a program that was compiled & linked using g++. Is there
    any other way to initialise a constant member variable using constructor
    while creating objects?
    There are two points: initialization and constructor body execution, the
    reason for const member functions to be initialized in the initializer is
    that by the time the constructor body is executed, the initialization is
    complete, and for const members, this is not possible because once you
    assign the value you can not change it afterwards.

    HTH

    Comment

    • utab

      #3
      Re: Constant member of a class

      There are two points: initialization and constructor body execution, the
      reason for const member functions to be initialized in the initializer
      Of course "const members" not const member functions

      Comment

      • Salt_Peter

        #4
        Re: Constant member of a class

        On Jul 4, 5:08 am, Anarki <Deepchan...@gm ail.comwrote:
        #include <iostream>
        using namespace std;
        >
        class ConstantMember
        {
        const int m_const;
        public:
        ConstantMember( int cons = 10):m_const(con s){}
        int getConst() const{ return m_const;}
        >
        };
        >
        int main()
        {
        ConstantMember X;//(20);
        cout << X.getConst();
        return 0;
        >
        }
        >
        the above is a program that was compiled & linked using g++. Is there
        any other way to initialise a constant member variable using
        constructor while creating objects?

        well yes, copy ctor which is generated for you in the code above and
        should look something like ...

        class C
        {
        const int m;
        public:
        C(int n = 10) : m(n) { }
        C(const C& copy) : m(copy.m) // ... this
        {
        std::cout << "copy\n";
        }
        int get() const { return m; }
        };

        int main()
        {
        C c;
        std::cout << c.get() << std::endl;
        C cop(c);
        std::cout << cop.get() << std::endl;
        }

        /*
        10
        copy
        10
        */

        Comment

        • Anarki

          #5
          Re: Constant member of a class

          #include <iostream>
          using namespace std;

          class ConstantMember1
          {
          const int m_const;
          public:
          ConstantMember1 (int cons = 20){m_const = cons;}
          int getConst() const{ return m_const;}

          };

          int main()
          {
          ConstantMember1 Y;
          cout << X.getConst();
          return 0;

          }


          Why doesn't this program compile? What is the difference between the
          constructor i gave in first post and this ( ConstantMember( ) and
          ConstantMember1 () )

          Comment

          • Anarki

            #6
            Re: Constant member of a class

            On Jul 14, 3:07 pm, Anarki <Deepchan...@gm ail.comwrote:
            #include <iostream>
            using namespace std;
            >
            class ConstantMember1
            {
                    const int m_const;
                    public:
                    ConstantMember1 (int cons = 20){m_const = cons;}
                    int getConst() const{ return m_const;}
            >
            };
            >
            int main()
            {
                    ConstantMember1 Y;
                    cout << X.getConst();
                    return 0;
            >
            }
            >
            Why doesn't this program compile? What is the difference between the
            constructor i gave in first post and this ( ConstantMember( ) and
            ConstantMember1 () )
            sorry there is a typing mistake in the above code its Y.getConst();
            but i have typed X.getConst();

            Guys is there way to edit my post where i have made mistakes like the
            above?

            Comment

            • Ian Collins

              #7
              Re: Constant member of a class

              Anarki wrote:
              #include <iostream>
              using namespace std;
              >
              class ConstantMember1
              {
              const int m_const;
              public:
              ConstantMember1 (int cons = 20){m_const = cons;}
              int getConst() const{ return m_const;}
              >
              };
              >
              int main()
              {
              ConstantMember1 Y;
              cout << X.getConst();
              return 0;
              >
              }
              >
              >
              Why doesn't this program compile?
              Too many bugs? Your compiler errors should have been clear enough.

              The only way to initialise a const member is with an initialiser list.
              What is the difference between the
              constructor i gave in first post and this ( ConstantMember( ) and
              ConstantMember1 () )
              You used an initialiser list.

              --
              Ian Collins.

              Comment

              • Ian Collins

                #8
                Re: Constant member of a class

                Anarki wrote:
                >
                Guys is there way to edit my post where i have made mistakes like the
                above?
                No. Cut and paste code you have tried rather than typing it into the
                message.

                --
                Ian Collins.

                Comment

                • Anarki

                  #9
                  Re: Constant member of a class

                  On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                  Anarki wrote:
                  #include <iostream>
                  using namespace std;
                  >
                  class ConstantMember1
                  {
                          const int m_const;
                          public:
                          ConstantMember1 (int cons = 20){m_const = cons;}
                          int getConst() const{ return m_const;}
                  >
                  };
                  >
                  int main()
                  {
                          ConstantMember1 Y;
                          cout << X.getConst();
                          return 0;
                  >
                  }
                  >
                  Why doesn't this program compile?
                  >
                  Too many bugs?  Your compiler errors should have been clear enough.
                  look friend i know the error is due to the constant variable in class.
                  But why should the compiler raise error? Am looking for more detailed
                  explanation.
                  >
                  The only way to initialise a const member is with an initialiser list.
                  >
                  What is the difference between the
                  constructor i gave in first post and this ( ConstantMember( ) and
                  ConstantMember1 () )
                  >
                  You used an initialiser list.
                  i understand i used initialiser list but whats happening inside the
                  initialiser list? What does the compiler do when it see the
                  initialiser list. I think u didn't get what i meant. i explain a bit
                  more what's the difference between a normal constructor and
                  constructor that uses initialiser list.
                  >
                  --
                  Ian Collins.
                  The answer you gave me was just the technical term/terminology of the
                  example i posted. But what exactly is happening inside this
                  initialiser list?? Some one please help......

                  Comment

                  • Puppet_Sock

                    #10
                    Re: Constant member of a class

                    On Jul 14, 10:07 am, Anarki <Deepchan...@gm ail.comwrote:
                    [snip]
                    Why doesn't this program compile?
                    >
                    Too many bugs?  Your compiler errors should have been clear enough.
                    >
                    look friend i know the error is due to the constant variable in class.
                    But why should the compiler raise error? Am looking for more detailed
                    explanation.
                    Did you read the post by utab that appeared here 10 days ago?
                    It's still on Google, so you could find it if you looked.
                    Socks

                    Comment

                    • Ian Collins

                      #11
                      Re: Constant member of a class

                      Anarki wrote:
                      On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                      >You used an initialiser list.
                      i understand i used initialiser list but whats happening inside the
                      initialiser list? What does the compiler do when it see the
                      initialiser list. I think u didn't get what i meant. i explain a bit
                      more what's the difference between a normal constructor and
                      constructor that uses initialiser list.
                      What does you book tell you? This is pretty basic stuff. If you want
                      to see exactly what your compiler does, look at the generated assembler.

                      --
                      Ian Collins.

                      Comment

                      • Anarki

                        #12
                        Re: Constant member of a class

                        On Jul 15, 1:08 am, Ian Collins <ian-n...@hotmail.co mwrote:
                        Anarki wrote:
                        On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                        You used an initialiser list.
                        i understand i used initialiser list but whats happening inside the
                        initialiser list? What does the compiler do when it see the
                        initialiser list. I think u didn't get what i meant. i explain a bit
                        more what's the difference between a normal constructor and
                        constructor that uses initialiser list.
                        >
                        What does you book tell you?  This is pretty basic stuff.  If you want
                        to see exactly what your compiler does, look at the generated assembler.
                        >
                        --
                        Ian Collins.
                        Sorry Ian am not a good programmer. I dunno how to get my hands on the
                        generated assembler and if at all i get my hands on it i dont think i
                        can understand it. Anyways i am gonna search for the stuff Puppet_sock
                        said about utab.

                        Comment

                        • Anarki

                          #13
                          Re: Constant member of a class

                          Did u mean his post in this thread? if so

                          Does it really mean that initialization of constant member variable
                          occurs before the execution of constructor(usi ng initialiser list).

                          Again, does that mean a member variable of a object get initialized
                          even before the object is constructed?

                          Clearing basics of C++ looks real chaos(for me) :)

                          Comment

                          • Ian Collins

                            #14
                            Re: Constant member of a class

                            Anarki wrote:
                            Did u mean his post in this thread? if so
                            >
                            Please don't used txt speak on Usenet.
                            Does it really mean that initialization of constant member variable
                            occurs before the execution of constructor(usi ng initialiser list).
                            >
                            Yes, members in the initialisation list are initialised before the body
                            of the constructor is run.
                            Again, does that mean a member variable of a object get initialized
                            even before the object is constructed?
                            >
                            No, the initialisation is one phase of the object's construction.

                            --
                            Ian Collins.

                            Comment

                            Working...