[webkit-dev] On returning mutable pointers from const methods
pkasting at google.com
Fri Oct 26 09:06:22 PDT 2012
On Fri, Oct 26, 2012 at 8:27 AM, Rik Cabanier <cabanier at gmail.com> wrote:
> It is valid for a const method to return you a new object ie a const
> factory object.
> In that case, const-ness would not be desired.
Not really. The point of this thread is that such functions may not modify
an object's state themselves, but they vend access that can be used by the
caller to modify it.
Consider for example:
Child* Parent::getNewChild() const;
Assuming the Parent doesn't have a list of its children (questionable), we
can implement this without mutable pointers. But then a caller can do:
Child* child = parent->getNewChild();
If you generalize this you find there are very, very few cases where a
const object can vend a non-const pointer that cannot possibly be used to
change the state of the world the const object sees. Which is why the rule
of thumb suggested in this thread is safer and easier than trying to reason
about individual cases.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the webkit-dev