# C++：模板函數需要類型轉換時使用友元(friend)模板函數

1. 直接在友元函數內部, 實現代碼(inline);

2. 在友元函數內部, 實現一個模板函數, 在把模板函數在外部實現.

`/*  * test.cpp  *  *  Created on: 2014.04.22  *      Author: Spike  */      /*eclipse cdt, gcc 4.8.1*/      #include <iostream>        template<typename T> class Rational;        template<typename T>  const Rational<T> doMultiply (const Rational<T>& lhs,      const Rational<T>& rhs);        //有理數//更多精彩內容：http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/template<typename T>  class Rational {      /*friend const Rational<T> operator* (const Rational<T>& lhs,         const Rational<T>& rhs) {         return Rational<T>(lhs.numerator()*rhs.numerator(),                 lhs.denominator()*rhs.denominator());     } //方法1*/    friend const Rational<T> operator* (const Rational<T>& lhs,          const Rational<T>& rhs) {          return doMultiply(lhs, rhs);      } //方法2  public:      Rational(const T& numerator = 0, const T& denominator = 1) :          m_n(numerator), m_d(denominator) {}      const T numerator() const {return m_n;};      const T denominator() const {return m_d;};      const T value() {return (m_n/m_d);}  private:      T m_n;      T m_d;  };        template<typename T>  const Rational<T> doMultiply (const Rational<T>& lhs,          const Rational<T>& rhs) {      return Rational<T>(lhs.numerator()*rhs.numerator(),              lhs.denominator()*rhs.denominator());  }        int main(void) {      Rational<double> oneFourth(1, 4);      Rational<double> result;      result = oneFourth * 3.5;      result = 3.5 * oneFourth;      std::cout << "result = " << result.value() << std::endl;            return 0;  }`

`result = 0.875`

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD