Effective C++ 读书笔记五
2017-05-14 15:41
316 查看
Effective C++ 读书笔记(五)
版权声明:本文为 cheng-zhi 原创文章,可以随意转载,但必须在明确位置注明出处!让自己习惯 C++ - 条款 03
尽可能的使用constconst允许你指定一个语义约束:指定一个对象不可被改变,下面是 const 的几种常见的使用方法:
const 修饰指针
char greeting[] = "Hello"; // non-const pointer, non-const data char *p = greeting; // non-const pointer, const data const char *p = greeting; // const pointer, non-const data char *p const = greeting; // const pointer, const data const char * const p = greeting; // const iter, non-const data const std::vector<int>::iterator iter; // non-const iter, const data std::vector<int>::const_iterator iter;
const 修饰函数返回值
// 将函数的返回值声明为 const, 可以防止返回值被恶意赋值 const MyType function();
const 修饰函数参数
// 表示参数 x 在函数内部不能被改变 MyType function(const int x);
const 修饰成员函数
// 该成员函数可以操作 const 对象 MyType function(const int x) const; // 两个成员函数如果常量性不同,可以被重载 char& operator[](std::size_t position) const;// 针对 const 对象 char& operator[](std::size_t position); // 针对 non-const 对象
如何取消 const 属性?
使用 mutable 去掉 const 属性。// 这个成员变量可以在 const 成员函数内被更改 mutable bool flag;
如何在 const
函数和 non-const
函数之间避免代码重复?
如果 const函数和
non-const函数的函数定义基本相同,我们可以使用
non-const函数来间接调用
const函数来减少重复的代码,这其中涉及到两次
const转型。
// const 函数 const char& operator[](std::size_t position) const { ... } // non-const 函数 char& operator[](std::size_t position) { // 1. 将 *this 转换成 const ClassName& 类型,使得后面的 [position] 得以调用到 const 版本的函数 // 2. 从 const operator[] 中移除 const 属性。 return const_cast<char&>(static_cast<const ClassName&>(*this)[position]); }
原则:
1. 将事实上确实不可变的东西声明为
const,可以帮助编译器侦测出错误用法。
2. 当
const和
non-const成员函数的定义等价时,让
non-const调用
const可以避免代码重复。
尽可能的使用 const
About Me
cheng-zhi:C / C++GitHub:cheng-zhi
个人主页:cheng-zhi
相关文章推荐
- Effective C++ 读书笔记之Part6.Inheritance and Object-Oriented Design
- Effective C++ 读书笔记之Part9.Miscellany
- Effective C++ 读书笔记二
- effective C++(第三版)读书笔记一——条款一:视C++为一个语言联邦
- 读书笔记《Effective C++》条款08:别让异常逃离析构函数
- Effective C++ 读书笔记(七) 模板与泛型编程
- 读书笔记 Effective C++
- Effective C++ 读书笔记之implemenations(2)
- 【Effective C++ 读书笔记】条款02: 尽量以 const, enum, inline 替换 #define
- 《Effective C++》读书笔记
- 《Effective C++》读书笔记之item27:尽量少做转型动作
- 《effective C++》读书笔记(三)
- 《Effective C++》读书笔记
- Effective C++ 读书笔记(5-10):内存管理
- Effective C++ 读书笔记(八)
- Effective C++ 读书笔记_1:构建全空对象数组/带参对象数组/Operator /Placement/new/指针Cast/分配一片内存
- Effective c++ 读书笔记
- 《Effective C++》读书笔记III
- 读书笔记 Effective C++: 04 设计与声明
- effective C++ 读书笔记(下)