   const member functions
  Let's say that I have an runway, and I have a queue of planes waiting to take off.  Suppose that the only functions I have follow.  I removed everything else because this is an exercise in how classes relate to each other.  The following classes and functions are supposed to used.class queue{public:void serve();bool empty();protected:int count;};void queue::serve(){count--;}bool queue::empty(){if (count == 0)return true;else return false;}class runway{public:int count_planes () const;private:Queue takeOff;}I need a function that is able to count the planes in Queue takeoff.  Since the function count_planes() is const, it cannot change any data members of class runway.  Does this imply that I cannot change any data members of takeOff as well?For example, suppose I implemented count_planes() like so:int runway::count_planes() const{int count = 0;while (!takeOff.empty()    //This will return true when the queue is empty and false otherwise.  This call should be legal because it does not alter members of takeOff.{takeOff.serve(); //Queue contains a protected data member count.  Serve will decrease this count value, thus altering the data of queue takeOff.count++;}return count}Is the following function legal?  After all, data inside takeOff is being changed when I call takeOff.serve()Alternatively, since the variable count inside queue is protected, couldn't I just call planes.count?EDIT: scratch that last paragraph.  Queue isn't inherited, so of course planes.count cannot be called.  That would be similar to saying planes.count in main.EDIT2:  I included some more code so things are easier to understand[Edited by - zoner7 on May 6, 2010 8:42:44 PM]
  takeOff is a member, so only its const member functions can be called. serve() may be called if it's const. count may not be modified.Access modifiers (protected/private/...) do not matter.const does not check whether anything gets modified or not. It merely checks the modifiers of functions. const variables cannot be modified anyway.The important part of const correctness is that it propagates. By making count_planes() const, the compiler can check that one does not accidentally modify other members.   ;  Doesn't queue have a function that simply returns the count? std::queue certainly does, as should any reasonable queue, and its a const function so there isn't any problem calling it.If 'serve' decreases the count, it implies that something is also being removed from the queue -- so even if you get the remaining items and compensate to satisfy the definition of 'count_planes', you're still loosing something.If you called your implimentation of count_planes based on 'serve' you'd see that the count will decrease by one each time the function is called Queue will be left empty since you've served all the planes out of it, resulting in a count of 0 each subsequent time -- which is neither what you want, nor logically consistant in any way.[edit] Missed that 'while' loop.   ;Quote:Original post by RavyneIf you called your implimentation of count_planes based on 'serve' you'd see that the count will decrease by one each time the function is called Queue will be left empty since you've served all the planes out of it, resulting in a count of 0 each subsequent time -- which is neither what you want, nor logically consistant in any way.[edit] Missed that 'while' loop.I realized that the queue would be empty.  It's simply an assignment.  For all practical purposes, such an implementation would make no sense at all.  The question is, however, am I allowed to change count inside this const count_planes() function?EDIT: hmm... VS gives me the following error when i try to compile: error C2662: 'queue::empty' : cannot convert 'this' pointer from 'const queue' to 'queue &'it's happy when I remove the const, however.;Quote:Original post by zoner7The question is, however, am I allowed to change count inside this const count_planes() function?No.It's not related to changing, but to how function is declared. empty() could be marked const, but serve cannot be since it modifies count.Quote:EDIT: hmm... VS gives me the following error when i try to compile: error C2662: 'queue::empty' : cannot convert 'this' pointer from 'const queue' to 'queue &'Which is a very C++-ish way of saying that const function is trying to call a non-const function.Compiler is basically saying the above code is has a bug.There are two ways to work around this issue, but none of them are relevant, since they negate the const-correctness.   ;  so then I suppose my teacher gave us a problem that can't be solved...Are there other compilers that would allow you to compile this code?   ;Quote:Are there other compilers that would allow you to compile this code?C++ is C++. It's true that different compilers will behave differently in some cases (due to bugs, non-standard extensions, etc.), but I doubt there are any (decent) C++ compilers that would compile your code as it is now. Anyway, how would switching to a broken compiler help you exactly?IMO, it doesn't really make sense for a function called count_planes() to modify the object on which it's called. I know you said you removed some stuff - is there a lot more to the assignment than what you've posted?Also, queue::empty() should be marked const, and should (or at least could) be implemented as follows:bool queue::empty() const{    return count == 0;};Quote:Original post by jykQuote:Are there other compilers that would allow you to compile this code?C++ is C++. It's true that different compilers will behave differently in some cases (due to bugs, non-standard extensions, etc.), but I doubt there are any (decent) C++ compilers that would compile your code as it is now. Anyway, how would switching to a broken compiler help you exactly?I was more just curious if the compiler that we use in class would compile this code.  My professor is nothing short of brilliant, but it's not like she specializes in c++, so she isn't necessarily familiar with all of its nuances, especially if the compiler isn't correcting her errors.  We type all of our code in emacs and compile using g++.  I only use VS for my own coding.Original post by jykQuote:IMO, it doesn't really make sense for a function called count_planes() to modify the object on which it's called. I know you said you removed some stuff - is there a lot more to the assignment than what you've posted?I don't think it makes a ton of sense either... It is just thinking exercise, though.  Clearly she wants us to act as a client who does not have access to the private data and who thus needs to solve a problem using member functions. All the code was on paper, so I essentially typed up the bare minimum, just the code that would be needed to solve this particular part of the assignment.  I guess will go speak with her and mention that, as the class is currently written, this part of the assignment is unsolvable.Quote:Also, queue::empty() should be marked const, and should (or at least could) be implemented as follows:bool queue::empty() const{    return count == 0;}oops. I transcribed that one wrong.  Thank you;  So I found a solution to the problem, but I still don't understand why it does not violate const correctness.int runway::count_planes() const{int count = 0;queue temp = takeOff;while (!temp.empty() {temp.serve();    count++;}return count}In this solution, we still have a const function calling a non-const function.  How come this is legal?   ;  Because temp isn't const? Inside a const member function, only *this is const. Local variables are unaffected.   