标签:
看看下面这个例子:
1 template<typename T> 2 class Rational{ 3 public: 4 Rational(const T & numerator, const T & denominator); 5 const T numerator()const; 6 const T denominator() const; 7 }; 8 template<typename T> 9 const Rational<T> operator*(const Rational<T>& lhs, 10 const Rational<T>& rhs); 11 Rational<int> oneHalf(1,2); 12 Rational<int> result = oneHalf*2;
1 template<typename T> 2 class Rational{ 3 public: 4 Rational(const T & numerator, const T & denominator); 5 const T numerator()const; 6 const T denominator() const; 7 friend 8 const Rational operator*(const Rational<T> & lhs, 9 const Rational<T> & rhs); 10 };
1 friend 2 const Rational<T> operator*(const Rational<T> & lhs, 3 const Rational<T> & rhs);
const Rational<T> & rhs);
1 template<typename T> 2 class Rational{ 3 public: 4 Rational(const T & numerator, const T & denominator); 5 const T numerator()const; 6 const T denominator() const; 7 friend 8 const Rational<T> operator*(const Rational<T> & lhs, 9 const Rational<T> & rhs) 10 { 11 return Rational(lhs.numerator() * rhs.numerator(), 12 lhs.denominator() * rhs.denominator()); 13 } 14 };
标签:
原文地址:http://www.cnblogs.com/-wang-cheng/p/4889821.html