Recently I've inspected a huge amount of legacy C++ code and found something I've never seen before in production C++ code:
class Foo
{
public:
void Bar()
{
std::cout << "Hello from Bar()!" << std::endl;
}
void Bar() const
{
const_cast<Foo*>(this)->Bar();
}
};
Is this a huge anti-pattern? I mean, the function is either const or non-const, what's the point of providing two versions? Is this some kind of 'const-correctness cheat', that allows to invoke const functions is situations like this:
void InvokeBar(const Foo& foo)
{
// oh boy! I really need to invoke a non-const function on a const reference!
foo.Bar();
}