Why do I get an error on line 27 (cannot convert 'this' pointer from 'const myClass' to 'myClass &') if I leave out the 'const' qualifier for myFunc01. The function is returning mc2, not mc1, so there is no danger of exposing the const mc1 parameter to changes through a return value. And even if the function did return mc1, it would be a copy only, not a reference since myFunc01's return type is 'myClass', not 'myClass &'. So I don't understand why the compiler requires myFunc01 to be const.
const myClass mc1;
// because mc1 is const, you cannot do things which might change its state
// this includes writing to its members, and calling non-const member functions
// (since non-const member functions are permitted to change members)
mc1.myFunc01( x, y ); // this will fail unless myFunc01 is a const member function
// because mc1 is const
// on the other hand, since mc2 is nonconst
// a non-const object can call const and non-const member functions just fine
mc2.myFunc01( x, y ); // no problem!
That's why you need the const qualifier for the function.
Thanks Disch. Suddenly it seems obvious now that you put it that way. I was overthinking it, but I see now what you mean. So the compiler doesn't have time to check if the function actually changes any data members, it just sees that the function is not const, so therefore the function COULD change data members, therefore that is too dangerous and an error. Thanks Disch and thanks guestgulkan, sorry guestgulkan I was not more clear on how to duplicate the error.