class A
{
...
public:
shared_ptr<Logger> GimmeLogger () const
{
return m_logger;
}
private:
shared_ptr<Logger> m_logger;
};
In class A, should GimmeLogger be const or non-const?
It would make sense to be const because it is a simple getter that doesn’t modify *this (syntactic const).
But on the other hand, it returns a non-const pointer to another object that it owns (semantically non-const).
If you make that non-const, then you cannot write this:
So if you want to write this; that is, if you want to call
GimmeLoggeron const object, then makeGimmeLoggera const member function, because you cannot invoke a non-const member function, on const object. However, you can invoke a const member function, on non-const object (as well as on const object).Inside a const member function, every member is semantically const objects. So the type of
m_loggerin the function becomesconst share_ptr<const m_logger>. So change the return type accordingly.