If you cast
b
to
const Blah&
then you can call only methods declared as
const
, i.e. that cannot modify the internal state of the object. In this case the choice is for
const Thing* GetThing() const
.
But it will continue to work if you write:
class Thing
{
public:
Thing() {}
...
};
class Blah
{
public:
Thing* GetThing() const;
private:
Thing* GetThing();
};
void anotherclass::function(Blah& b)
{
const Thing* thing = b.GetThing();
}
This will continue to work because is safe to implicitly add the
const
attribute to a variable, whereas it is not safe the opposite.
In your code, the compiler choose which version of your method to use depending on the fact that
b
is
const
or not.