Avoiding duplication of code in const and non-const getter methods
suggest changeIn C++, methods that differ only by a const qualifier can be overloaded. Sometimes there may be a need for two versions of a getter that return a reference to some member.
Let Foo be a class that has two methods that perform identical operations and return a reference to an object of type Bar:
class Foo
{
public:
Bar& GetBar(/* some arguments */)
{
/* some calculations */
return bar;
}
const Bar& GetBar(/* some arguments */) const
{
/* some calculations */
return bar;
}
// ...
};
The only difference is that the non-const overload returns a reference that can modify the object, whereas the const overload returns a const reference.
To avoid the code duplication, there is a temptation to call one method from another. However, we cannot call the non-const method from the const one. But we can call the const method from the non-const one. That will require us to use const_cast to remove the const qualifier.
The solution is:
struct Foo
{
Bar& GetBar(/*arguments*/)
{
return const_cast<Bar&>(const_cast<const Foo*>(this)->GetBar(/*arguments*/));
}
const Bar& GetBar(/*arguments*/) const
{
/* some calculations */
return bar;
}
};
In the code above, we call the const version of GetBar from the non-const GetBar by casting this to a const type: const_cast<const Foo*>(this). Since we call the const method from a non-const method, the object itself is non-const, and casting away const is allowed. Modifying the result would be undefined behavior if the object were actually const.
Examine the following more complete example:
#include <iostream>
class Student
{
public:
char& GetScore(bool midterm)
{
return const_cast<char&>(const_cast<const Student*>(this)->GetScore(midterm));
}
const char& GetScore(bool midterm) const
{
if (midterm)
{
return midtermScore;
}
else
{
return finalScore;
}
}
private:
char midtermScore;
char finalScore;
};
int main()
{
// non-const object
Student a;
// We can assign to the reference. Non-const version of GetScore is called
a.GetScore(true) = 'B';
a.GetScore(false) = 'A';
// const object
const Student b(a);
// We still can call GetScore method of const object,
// because we have overloaded const version of GetScore
std::cout << b.GetScore(true) << b.GetScore(false) << '\n';
}