Thread: Beginner's question: const operator++() ?

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    Beginner's question: const operator++() ?

    Hi, another beginner's question...

    I am reading Sams Teach Yourself C++ by Jesse Liberty and Rogers Cadenhead.

    This is an example from their book:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class Counter {
    public:
      Counter();
      ~Counter() {}
      int getValue() const { return value; }
      void setValue (int x) { value = x; }
      void increment() { ++value; }
      const Counter& operator++();
    
    private:
      int value;
    };
    
    Counter::Counter():value(0) {}
    
    const Counter& Counter::operator++() {
      ++value;
      return *this;
    }
    
    
    int main() {
      Counter c;
      cout << "c is " << c.getValue() << endl;
      c.increment();
      cout << "c is " << c.getValue() << endl;
      ++c;
      cout << "c is " << c.getValue() << endl;
      Counter a = ++c;
      cout << "a is " << a.getValue() << endl;
      cout << "c is " << c.getValue() << endl;
      return 0;
    }
    My initial confusion was:
    How can the function be const if it is changing the value of the member variable?

    Then I realized that it is making the returned object const, not the object that the operator is used upon. (Can you confirm this is correct statement?).

    If this is in fact true so far:
    Why would you even what to have a const obj to assign to another object?
    I mean, it is not like the returned object would have to be const so that it could be assigned to a const obj, after all, you can assign to a const object anyway, regardless of the object on the right hand side.

    Unless this is meant for the particular case where you are creating a const obj.

    Is this what all this const operator declaration is really about? To support this case?

    I really appreciate your clarification.

    Many thanks!
    Eovento

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by yaraeovento
    My initial confusion was:
    How can the function be const if it is changing the value of the member variable?

    Then I realized that it is making the returned object const, not the object that the operator is used upon. (Can you confirm this is correct statement?).
    If the member function is declared const, then the const keyword would have been placed after the parameter list. As you can see, that is not so, and indeed the const applies to the return type, i.e., the return type is const Counter&.

    EDIT:
    Quote Originally Posted by yaraeovento
    Why would you even what to have a const obj to assign to another object?
    The aim is probably to forbid a statement like:
    Code:
    Counter& a = ++c;
    Last edited by laserlight; 05-21-2013 at 02:01 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Many many thanks for you help. I understand now.

    All the best!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator[] difference of const and non-const
    By DynV in forum C++ Programming
    Replies: 31
    Last Post: 07-11-2012, 11:38 PM
  2. Beginner Question about OR operator
    By Enthusiast in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2011, 09:04 PM
  3. question about const pointers and const ints
    By WarDoGG in forum C Programming
    Replies: 9
    Last Post: 01-08-2011, 02:11 PM
  4. #define versus const (a beginner's question)
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-11-2002, 12:38 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM

Tags for this Thread