# C++: error: passing 'const X' as 'this' argument discards qualifiers [-fpermissive]

**Code:** `discards qualifiers` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

A const object (or const reference) is calling a method that is not declared const, which could modify it.

## Meaning

Calling `obj.method()` on a `const Obj&` is only allowed if `method()` is marked `const`. Mark every method that does not change the object `const`; that is also what makes them usable from const references and in containers.

## Common causes

- Member function that only reads data is not declared `const`
- Const reference or pointer parameter calling a non-const method
- Range-for over a const container using non-const methods
- `operator[]` or `begin()` without a const overload

## Quick fix

1. Add `const` after the parameter list: `int size() const;`
2. Change `T&` to `const T&` only if the method truly does not modify
3. Provide both const and non-const overloads when needed

## Detailed fix by platform

### C++

1. Declare read-only methods as `int size() const { return n_; }`
2. If it caches data, mark the cache `mutable`

## Code examples

### Make read-only methods const

```cpp
struct Stack {
    int top() const { return v[n - 1]; }   // const: only reads
    void push(int x) { v[n++] = x; }       // modifies, so non-const
    int v[16]; int n = 0;
};

void print(const Stack& s) {
    s.top();          // OK
    // s.push(1);     // error: passing 'const Stack' as 'this' argument discards qualifiers
}
```

## How to diagnose

1. **Object** — Is the object const?
2. **Method** — Is the called method declared const?
3. **Intent** — Does the method modify state?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/discards-qualifiers)
