in C++ classes share the same name, body, and return type
(except for the
const keyword), thus causing code duplication.
The problem is described in
this StackOverflow topic,
and somewhere else on the Internet. The obvious solution is to refactor the code
in order to avoid duplication while preserving const-correctness and semantics,
but sometimes this is not possible or not convenient.
A popular (and effective) solution
const_cast and a
In this post, I suggest an alternative (and I think safer) solution.