本文主要是介绍条款24:若所有参数皆需类型转换,请为此采用非成员函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
设计一个表示有理数的类时,允许从整数隐式转换为有理数是有用的:
class Rational {
public:Rational(int numerator = 0, // 该构造函数没有explicit限制;int denominator = 1); int numerator() const; int denominator() const; const Rational operator*(const Rational& rhs) const;
private:int n,d;
};Rational oneEighth(1, 8);
Rational oneHalf(1, 2);
Rational result = oneHalf * oneEighth; // 正确
result = result * oneEighth; // 正确
result = oneHalf * 2; // result = oneHalf.operator*(2); 正确
result = 2 * oneHalf; // result = 2.operator*(oneHalf); 错误!
乘法应该是可交换的,但是由于参数的隐式类型转换只发生在参数列表中,所以2 * oneHalf会报错,由于2是int类型的参数。
这时候我们可以将operator重载函数修改为非成员函数,编译器还会寻找非成员运算符(即命名空间或全局作用域中的运算符):
const Rational operator*(const Rational& lhs, const Rational& rhs)
{return Rational(lhs.numerator() * rhs.numerator(),lhs.denominator() * rhs.denominator());
}
Rational oneFourth(1, 4);
Rational result;
result = oneFourth * 2; // 正确
result = 2 * oneFourth; // 欧耶,它起作用了!
#include <iostream>class Rational {
public:Rational(int numerator = 0, // 该构造函数没有explicit限制;int denominator = 1):n{numerator},d{denominator}{ }int numerator() const {return n;}int denominator() const { return d;}//const Rational operator*(const Rational& rhs) const {// return Rational(this->n*rhs.n,this->d*rhs.d);//}void print() const {if(n%d==0)std::cout << n /d << std::endl;else if(d%n==0)std::cout << 1 << "/" << d/n << std::endl;elsestd::cout << n << "/" << d << std::endl;}
private:int n, d;
};
const Rational operator*(const Rational& lhs, const Rational& rhs)
{return Rational(lhs.numerator() * rhs.numerator(),lhs.denominator() * rhs.denominator());
}int main()
{Rational oneEighth(1, 8);Rational oneHalf(1, 2);Rational result = oneHalf * oneEighth; // 正确result = result * oneEighth; // 正确result.print();result = oneHalf * 2; // result = oneHalf.operator*(2); 正确result.print();result = 2 * oneHalf; // result = 2.operator*(oneHalf); result.print();
}
这篇关于条款24:若所有参数皆需类型转换,请为此采用非成员函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!