I am learning about classes in C++. I came across the following statement from the standard:
During overload resolution, non-static cv-qualified member function of class X is treated as a function that takes an implicit parameter of type lvalue reference to cv-qualified X if it has no ref-qualifiers or if it has the lvalue ref-qualifier. Otherwise (if it has rvalue ref-qualifier), it is treated as a function taking an implicit parameter of type rvalue reference to cv-qualified X.
The above statement seems to imply that for a const
qualified non-static member function of a class X
will have an implicit parameter of type const X&
.
But then i also came across:
The type of
this
in a member function of classX
isX*
(pointer to X). If the member function is cv-qualified, the type of this iscv X*
(pointer to identically cv-qualified X). Since constructors and destructors cannot be cv-qualified, the type of this in them is alwaysX*
, even when constructing or destroying a const object.
So according to the above second quote the implicit this parameter for a const qualified non-static member function of class X
has type const X*
.
My question is that why is there such a difference. I mean during overload resolution for a const qualfied nonstatic member function, why is the implicit parameter considered as a const X&
and not simply a const X*
which seems to be the actual type of this
.
X*
parameter can be implicitly converted to abool
, whereas anX&
parameter cannot. So, using a reference would eliminate clashes with functions having abool
first argument. – Dowland