设为首页 加入收藏

TOP

Item 3: Use const whenever possible.(9)
2013-10-07 14:26:22 来源: 作者: 【 】 浏览:50
Tags:Item Use const whenever possible.

The cast that adds const is just forcing a safe conversion (from a non-const object to a const one), so we use a static_cast for that. The one that removes const can be accomplished only via a const_cast, so we don’t really have a choice there. (Technically, we do. A C-style cast would also work, but, as I explain in Item 27, such casts are rarely the right choice. If you’re unfamiliar with static_cast or const_cast, Item 27 contains an overview.)

On top of everything else, we’re calling an operator in this example, so the syntax is a little strange. The result may not win any beauty contests, but it has the desired effect of avoiding code duplication by implementing the non-const version of operator[] in terms of the const version. Whether achieving that goal is worth the ungainly syntax is something only you can determine, but the technique of implementing a non-const member function in terms of its const twin is definitely worth knowing.

Even more worth knowing is that trying to do things the other way around — avoiding duplication by having the const version call the non-const version — is not something you want to do. Remember, a const member function promises never to change the logical state of its object, but a non-const member function makes no such promise. If you were to call a non-const function from a const one, you’d run the risk that the object you’d promised not to modify would be changed. That’s why having a const member function call a non-const one is wrong: the object could be changed. In fact, to get the code to compile, you’d have to use a const_cast to get rid of the const on *this, a clear sign of trouble. The reverse calling sequence — the one we used above — is safe: the non-const member function can do whatever it wants with an object, so calling a const member function imposes no risk. That’s why a static_cast works on *this in that case: there’s no const-related danger.

As I noted at the beginning of this Item, const is a wonderful thing. On pointers and iterators; on the objects referred to by pointers, iterators, and references; on function parameters and return types; on local variables; and on member functions, const is a powerful ally. Use it whenever you can. You’ll be glad you did.

Things to Remember

Declaring something const helps compilers detect usage errors. const can be applied to objects at any scope, to function parameters and return types, and to member functions as a whole.

Compilers enforce bitwise constness, but you should program using conceptual constness.

When const and non-const member functions have essentially identical implementations, code duplication can be avoided by having the non-const version call the const version.


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Item 4: Make sure that objects .. 下一篇Item 3: Use const whenever poss..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: