Edit: Please read the question. The suggested duplicates don't answer my question. (I already read those answers before posting this question.)
If compiling the code below, MSVC spits out a quite well-known error message cannot convert 'this' pointer from 'const S' to 'S &'
.
We can remove the error by modifying the S::f
into a const
function, but my question is why the message is like that.
I thought the error message should be: cannot convert 'this' pointer from 'const S*' to 'S*'
. Why it's 'const S'
to 'S &'
, not 'const S*'
to 'S*'
?
(I read some answers such as this and this, just for the context of my curiosity.)
struct S {
void f(){}
};
int main() {
const S s;
s.f(); // 'void S::f(void)': cannot convert 'this' pointer from 'const S' to 'S &'
}