This article is one of <Effective C++> reading notes.
Part 4: Designs and Declarations
Item 24: Declare non-member function when type conversions should apply to all parameters.
Things To Remember
- If you need type conversions on all parameters to a function (include the one pointed to by this pointer), the function must be a non-member.
Key Points
- The opposite of a member function is a non-member function, not a friend function.
- Wherever you can avoid friend functions, you should, because, much as in real life, friends are often more trouble than they’re worth.
- 在设计 C++ 程序时,第一考虑的应该是怎样用最少的 C++ 知识来达成目地。重载操作法,对于不使用 template 的场合,多半不是必要的。所以,更偏向于先实现一个 const Rational MulRational(const Rational& lhs, const Rational& rhs) 的函数,再根据需要实现 operator* 调用它。
Code
|
|