Consider the following code (which hides all spheres in a collection from the scene):
void AbstractCanalRepresentation::HideFromScene() const noexcept
{
for (const auto& sphere : spheres_)
{
Renderer->RemoveActor(sphere);
}
Renderer->RemoveActor(segment_);
}
Where the Renderer
is a representation of the scene (reference to a third-party rendering library), spheres_
is a collection of objects this class owns.
CppCoreGuidelines tells us that logical const should be preferred over a physical one.
In this case, an author marked the method as const
despite of the fact it changes encapsulated objects visibility. So logical state changed (maybe), but the physical one didn’t. I’m totally agree with an author, but some another reviewers dont.
After a little argue, I’ve decided to ask it here. I think that const
must be here, because we dont change any of encapsulated objects, we dont have any mutable
attributes, etc. So, the method doesnt mutate an object state, but it mutates a third-party rendering library state which we dont care about, because it’s not an object state. We just store a reference to it.
Am I right?
1