const in classes

This is a discussion on const in classes within the C++ Programming forums, part of the General Programming Boards category; C++ beginner here. I'm going through Eckels' "Thinking in C++" and am a little confused about his explanation of const ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    const in classes

    C++ beginner here. I'm going through Eckels' "Thinking in C++" and am a little confused about his explanation of const in classes. He starts out by saying:

    "Thus, when you create an ordinary (non-static) const inside a class, you cannot give it an initial value. This initialization must occur in the constructor, of course, but in a special place in the constructor. Because a const must be initialized at the point it is created, inside the main body of the constructor the const must already be initialized."

    All well and good. So he goes on to introduce the constructor initializer list for this very purpose and says "This is the place to put all const initializations. " We're given this code snippet:

    Code:
    class Fred {
      const int size;
    public:
      Fred(int sz);
      void print();
    };
    
    Fred::Fred(int sz) : size(sz) {}
    void Fred::print() { cout << size << endl; }
    "size" is a const, so it's initialized in the constructor initializer list. But then he gives us another code snippet in which a non-cost data member is initialized in the constructor initializer list:

    Code:
    class StringStack {
      static const int size = 100;
      const string* stack[size];
      int index;
    public:
      StringStack();
      void push(const string* s);
      const string* pop();
    };
    
    StringStack::StringStack() : index(0) {
      memset(stack, 0, size * sizeof(string*));
    }
    Am I right in thinking that he didn't have to do this and that "index" could have been set in the body of the constructor function?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Post in the C++ forum next time; this is the C forum and your question certainly isn't related to C.

    Am I right in thinking that he didn't have to do this and that "index" could have been set in the body of the constructor function?
    Yes, it could have been, since index is an ordinary variable. But I think it might be slightly more efficient to initialize it in the initializer list (in theory, I'm sure it's optimized to be the same thing). It also makes it a bit more explicit what you are doing. If you see code like "index = 0", you can't immediately tell that you're initializing a member variable; whereas with the constructor initializer list, this is immediately obvious.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by dwks
    Post in the C++ forum next time; this is the C forum and your question certainly isn't related to C.
    I moved this thread but apparently you were too quick even for me
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Oops sorry, my mistake! I did actually intend to post this in the C++ section. My browser bookmark points to the C forum and usually I navigate to the C++ forum from there. I guess I got distracted and posted too early....thanks for the answer though, it certainly makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21