[EC++_item3]任何可能的时候都使用const

本文探讨了在C++编程中正确使用const的重要性。通过合理地应用const,可以帮助编译器检测到潜在的错误用法,并确保代码的一致性和安全性。文章强调了const可以应用于不同作用域的对象、函数参数、返回类型及成员函数。此外还提到了通过让非常量版本调用常量版本来避免代码重复的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Things to Rember

1.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.

将某些东西声明为const可帮助编译器侦测出错误用法。const可被施加于任何作用域内的对象、函数参数、函数

返回类型、成员函数本体。

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

编译器强制实施bitwise constness, 但你编写程序时应该使用“概念上的常量性”

3.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.

当const和non-const成员函数有着实质等价的实现时,令non-const版本调用const版本可避免代码重复。

posted on 2014-04-06 21:26 迷路的龙猫 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/pansj/p/EffectiveCPlusPlus_Item_3.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值