<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On May 31, 2011, at 10:54 AM, Peter Kasting wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div class="gmail_quote">On Mon, May 30, 2011 at 11:32 PM, Maciej Stachowiak <span dir="ltr"><<a href="mailto:mjs@apple.com">mjs@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div style="word-wrap:break-word"><div><div class="im"><div>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.</div>
</div></div></div></blockquote><div><br></div><div>Indeed.</div><div><br></div><div>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.</div>
<div><br></div><div>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.</div>
<div><br></div><div>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.</div></div></blockquote><br></div><div>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".</div><br><div>This explains both why mutable data members are ok (they are meant for cache/memoization purposes, and do not alter observable state) and why const methods returning non-const pointers to other objects in a mutually referencing graph are not.</div><div><br></div><div>Regards,</div><div>Maciej</div><div><br></div></body></html>