The complier tell me error C2662: 'Flower::GetColor' : cannot convert 'this' pointer from 'const Flower' to 'Flower &'
I search about error C2662, find that change the function GetColor with end of const:
the question is why error C2662 is about convert 'this' ? I dont try convert 'this'; And funciton 'GetColor()' I must need 'const' even if I not change the class value?
Yes, you do. Non-const member functions can only take non-const this parameters. The compiler is not smart enough to look at your function and tell whether or not you're modifying the object. It needs the const there so that it can be sure that the object will not change.
Consider that if you had a const int, you couldn't pass it to a function void (int &), because it requires that the parameter be non-const. This is exactly what's going on here.
It can be const when it does not modify any of its members.
1 2 3 4 5 6
class A {
int i;
public:
int f1() const { return i; } // legal because 'i' is not modified
void f2() const { ++i; } // error because it modifies 'i'
};