[webkit-dev] Do we have a style preference about const member functions?

Peter Kasting pkasting at chromium.org
Tue May 31 10:54:53 PDT 2011


On Mon, May 30, 2011 at 11:32 PM, Maciej Stachowiak <mjs at apple.com> wrote:

> A linked list node or tree node could useful have const methods, which give
> only const pointers/references to other nodes. If there is a reason const
> references to DOM nodes or renew objects are not useful, it is not due to
> the object graph participation itself, in my opinion.
>

Indeed.

The rule of thumb I use is that a const member function should only return
const-ref or pointer-to-const objects (whether as return values or
outparams).  This helps ensure that the method is logically const, not just
physically const, by preventing callers from using const methods to gain
handles they can use to modify supposedly-const objects.

It so happens that objects in trees/graphs tend to have functions that
return pointers to other objects much more frequently than do independent
data holders.  Thus Geoff's rule ends up approximating my rule, but is not
equivalent.

As to use of const in general, I would prefer to see more of it rather than
less, _assuming it is used only for logical constness_.  See Scott Meyers'
"Effective C++", item 3, for rationale.

PK
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20110531/987f2421/attachment.html>


More information about the webkit-dev mailing list