以pass-by-reference-to-const 替换pass-by-value 考虑以下class继承体系 class Person { public: Person(); // parameters omitted for simplicity virtual ~Person(); // see Item 7 for why this is virtual ... private: std::string name; std::string address; }; class Stud…
The Decision between Member and Non-member The binary operators = (assignment), [] (array subscription), -> (member access), as well as the n-ary () (function call) operator, must always be implemented as member functions, because the syntax of the l…
这个条款可以看成是条款24的续集,我们先简单回顾一下条款24,它说了为什么类似于operator *这样的重载运算符要定义成非成员函数(是为了保证混合乘法2*SomeRational或者SomeRational*2都可以通过编译,2不能同时进行隐式类型转换成某个Rational,再作this用). 所以我们一般将之定义成友元函数,像下面这样: class Rational { private: int numerator; int denominator; public: Rational(,…