Skip to main content
Explain the “discards qualifiers” error in C++ using the metaphor of a contractor who has signed a contract not to change instance variables, but then attempts to use subcontractor who hasn’t agreed to the same terms or signed the appropriate forms.
- The "discards qualifiers" error is difficult to decipher. The error reads as follows:
error: passing 'const Example' as 'this' argument of 'int Example::getValue()' discards qualifiers
- Using this metaphor the contract requires a const but it is not being given what it was promised.
-
- To fix this, you have to make the subcontractor agree to the same terms, to make the subcontractor const as well.
- The necessary fix to the Java example below is to make the accessor method, getValue, a
const method.
- Using this metaphor, isOddValue is the contractor, and getValue is the subcontractor who has not signed the appropriate contract.
- Example code:
class Example {
public:
Example();
int getValue() { return myValue;};
bool isOddValue() const;
private:
int myValue;
};
bool Example::isOddValue() const {
return (getValue() % 10 == 0);
}