Passing Const Objects as 'this' Argument: Qualifier Disqualification Error
In C , passing const objects as 'this' arguments to member functions can result in "passing 'const xxx' as 'this' argument of member function discards qualifiers" errors. This occurs because the compiler considers the possibility that non-const member functions may modify the object, which is prohibited for const objects.
Problem Analysis
In the provided code, the objects in the set are stored as const StudentT. When accessing member functions getId() and getName() within the loop, the compiler detects this issue since the objects are const and the member functions are not marked as const.
Solution
To resolve the error, the getId() and getName() functions must be made const:
int getId() const { return id; } string getName() const { return name; }
This allows the functions to be called on const objects without violating the const rules.
Additional Notes
inline bool operator< (const StudentT & s1, const StudentT & s2) { return s1.getId() < s2.getId(); }
The above is the detailed content of Why Does Passing a Const Object to a Non-Const Member Function Cause a Qualifier Disqualification Error in C ?. For more information, please follow other related articles on the PHP Chinese website!