《C 箴言:声明为非成员函数的时机》阐述了为什么只有 non-member functions(非成员函数)适合于应用到所有 arguments(实参)的 implicit type conversions(隐式类型转换),而且它还作为一个示例使用了一个 Rational class 的 operator* function。我建议你在阅读本文之前先熟悉那个示例,因为本文进行了针对《C 箴言:声明为非成员函数的时机》中的示例做了一个无伤大雅(模板化 Rational 和 operator*)的扩展讨论:
template class Rational { public: Rational(const T&. numerator = 0, // see《C 箴言:用传引用给const取代传值》for why params const T&. denominator = 1). // are now passed by reference
const T numerator() const. // see《C 箴言:避免返回对象内部构件的句柄》for why return const T denominator() const. // values are still passed by value, ... // Item 3 for why they’re const }.
·在写一个提供了 class template(类模板),而这个 class template(类模板)提供了一个函数,这个函数指涉到支持所有 parameters(参数)的 implicit type conversions(隐式类型转换)的 template(模板)的时候,把这些函数定义为 class template(类模板)内部的 friends(友元)。 src="/ncre2/js/wxgg.js">