The technical reason behind this behaviour is the the overload resolution.
Method declared as
class A {
void PrintMessage() { ... }
void PrintMessage() const { ... }
}
translate
behind the scenes to (this is conceptual, you can not see or call that):
static void A::PrintMessage(A * const this) { ... }
static void A::PrintMessage(A const * const this) { ... }
If the C++ compiler has to choose between function declarations, the overload resolution takes the
best match:
A varA = A();
varA.PrintMessage();
const A constA = A(); constA.PrintMessage();
this translates internally behind the scenes again into:
...
A::PrintMessage(&varA); ...
A::PrintMessage(&constA);
From a technical point of view: straight forward - no (other) magic involved ;-)
Cheers
Andi