C++ Primer 读书笔记 - 第十四章
2013-06-02 00:59
232 查看
1. An overloaded operator must have at least one operand of class or enumeration type.
2. Default arguments for overloaded operators are illegal, except for operator(), the function-call operator.
3. The assignment (=), subscript ([]), call (()), and member access arrow (->) operators must be defined as members. Defining any of these operators as a nonmember function is flagged at compile time as an error.
Like assignment, the compound-assignment operators ordinarily ought to be members of the class. Unlike assignment, they are not required to be so and the compiler will not complain if a nonmember compound-assignment operator is defined.
Other operators that change he state of their object or that are closely tied to their given type - such as increment, decrement, and dereference - usually should be members of the class.
Symmetric operators, such as the arithmetic, equality, relational, and bitwise operators, are best defined as ordinary nonmember functions.
4. < 和 == 的定义应该保持一致。
5. 定义下标操作的类,应该有两个版本:返回引用的非const成员,返回const引用的const成员。
6. 不用为ScreenPtr类定义默认构造函数,因此,一个ScreenPtr对象将总是指向一个Screen对象,不会有未绑定的ScreenPtr,这一点和内置指针不同。
6. Function Object
2. Default arguments for overloaded operators are illegal, except for operator(), the function-call operator.
3. The assignment (=), subscript ([]), call (()), and member access arrow (->) operators must be defined as members. Defining any of these operators as a nonmember function is flagged at compile time as an error.
Like assignment, the compound-assignment operators ordinarily ought to be members of the class. Unlike assignment, they are not required to be so and the compiler will not complain if a nonmember compound-assignment operator is defined.
Other operators that change he state of their object or that are closely tied to their given type - such as increment, decrement, and dereference - usually should be members of the class.
Symmetric operators, such as the arithmetic, equality, relational, and bitwise operators, are best defined as ordinary nonmember functions.
4. < 和 == 的定义应该保持一致。
5. 定义下标操作的类,应该有两个版本:返回引用的非const成员,返回const引用的const成员。
6. 不用为ScreenPtr类定义默认构造函数,因此,一个ScreenPtr对象将总是指向一个Screen对象,不会有未绑定的ScreenPtr,这一点和内置指针不同。
6. Function Object
class GT_cls { public: GT_cls(size_t val = 0) : bound(val) { } bool operator() (const string &s) { return s.size() >= bound; } private: std::string::size_type bound; }; count_if(words.begin(), words.end(), GT_cls(6)); count_if的原型是 template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, UnaryPredicate pred) { typename iterator_traits<InputIterator>::difference_type ret = 0; while (first!=last) { if (pred(*first)) ++ret; ++first; } return ret; } 由于类重载了()操作符,所以pred(*first)仍然可用。
相关文章推荐
- 读书笔记《C++ Primer》第五版——第十四章 重载运算与类型转换
- 《C++ Primer》读书笔记第十四章-1-重载IO、算术、关系等运算符
- 《C++ Primer》读书笔记第十四章-2-函数调用运算符
- 《C++ Primer》读书笔记第十四章-3-类型转换运算符、二义性
- C++ Primer 第五版: chapter 1 读书笔记
- 《c++ primer》读书笔记2
- 读书笔记:C++ primer 5th edition--chapter8.IO Lib
- 读书笔记:C++ primer 5th edition--chapter11.关联容器
- 读书笔记:C++ primer 5th edition--chapter14.重载运算与类型转换
- 《C++ Primer》读书笔记第十一章-2-关联容器操作
- 《C++ Primer》读书笔记第十二章-2-动态数组 And 文本查询程序
- 《C++ Primer 4th》读书笔记 第8章-标准IO库
- 明明白白c++之类的基本操作(c++ primer 的读书笔记 ,类对象, 类用户, 类成员的含义)
- 《C++ Primer》读书笔记-第三章 01 using声明
- C++ Primer 读书笔记 - 第二章
- C++ Primer 4th 读书笔记(第二部分)
- C++ Primer 读书笔记 - 第十三章
- C++ Primer 读书笔记 - 第十五章
- 《C++ Primer 5th》读书笔记6---再谈初始化
- C++ primer 读书笔记