class Fraction{
public:
Fraction(int num, int den = 1) : m_numerator(num), m_denominator(den) { }
Fraction operator + (const Fraction& f){
return Fraction(.....);
}
private:
int m_numerator;
int m_denominator;
}
Fraction f(3, 5);
Fraction d2 = f + 4; //调用non-explicit ctor 将4转为Fraction,这部分内容挺有趣的!
explicit-one-argument ctor
explicit不允许自动类型转换,通常用在构造函数前面(90%)。
class Fraction{
public:
explict Fraction(int num, int den = 1) : m_numberator(num), m_denominator(den) { }
operator double( ) const {return (double)(m_numerator / m_denominator)}
Fraction operator (const Fraction& f) { return Fraction(...) };
private:
int m_numerator;
int m_denominator;
};
Fraction f(3, 5);
Fraction d2 = f + 4; //不允许自动类型转换