[webkit-dev] On returning mutable pointers from const methods
Rik Cabanier
cabanier at gmail.com
Fri Oct 26 10:21:15 PDT 2012
On Fri, Oct 26, 2012 at 9:06 AM, Peter Kasting <pkasting at google.com> wrote:
> 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();
> child->parent->mutate();
>
this would only be possible if that parent object is casting away a 'const'
somewhere or accessing a global non-const object.
Maybe there should be a rule that 'mutable' or 'const_cast' should not be
allowed.
>
> 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.
>
> PK
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20121026/b0599388/attachment.html>
More information about the webkit-dev
mailing list