Possible Duplicate:
why private value of the obj can be changed by class instance?
Consider the following (partial) code:
class Group {
private:
int id;
public:
void set_id(int);
int get_id();
bool operator==(const Group&);
};
bool Group::operator==(const Group& g) {
if(g.id == this->id) { /* id is private? */
return true;
}
return false;
}
The code compiles and results seem proper. However, in the if
part of the operator overloading implementation, we are directly accessing the private member of its argument - const Group& g
, but isn't such an access invalid?