A New Krazy Checker for Methods Returning const refs
Thomas Zander
zander at kde.org
Sun Apr 15 08:08:20 BST 2007
On Saturday 14 April 2007 23:40, Jos van den Oever wrote:
> I'd even say that for most classes,
> returning const X& is to be preferred over returning X.
There is another reason that thiago did not note why its a bad idea to
return const references. I fixed the API of a class I made last week to
stop returning a const reference and promptly some code broke elsewhere.
The reason for the breakage was that the caller didn't want to copy and
ended up having a const Foo *foo; like pointer as his private member.
Which means that he was pointing at an internal member of my class for the
whole lifetime of his class.
And thats just bad design. Its bound to crash weirdly sooner or later.
Without returning const you get a nice compiler warning. And you avoid a
whole class of hard to fix bugs.
--
Thomas Zander
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070415/c0106040/attachment.sig>
More information about the kde-core-devel
mailing list