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

Geoffrey Garen ggaren at apple.com
Tue May 31 12:08:55 PDT 2011

> I agree that const should be used for "logical constness". The rule should not be merely "doesn't alter any data members of this object" but rather "does not alter observable state of this object or vend any type of pointer or reference by which observable state of this object could be altered".
> Precisely!

I like this explanation too. 

I'm trying to think of a simple way to explain / test whether something falls into the category of logical constness, since it can be ambiguous.

It occurred to me that a simple, though imperfect, test is just, "Is this function called by an owner of a const pointer / reference?" After all, a const member function is meaningless if nobody points to the class through a const pointer  / reference. For classes like DOM and render tree nodes, which have no meaningful const-pointer-owning clients, the answer is always no. For other classes, the answer is yes, but only if someone has found a meaningful use for a const pointer or reference to the class.

So, perhaps the real style question we should answer is when to use const pointers / references, since the answer to when to use const member functions will follow from it.

What are some good examples of existing code that meaningfully uses a const pointer or reference? (Something other than, say, the obligatory const& in a copy constructor.)

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

More information about the webkit-dev mailing list