怎样编写求两数和的函数 模板特化

 

版权声明

请尊重原创作品。转载请保持文章完整性,并以超链接形式注明原始作者“tingsking18”和主站点地址,方便其他朋友提问和指正。

 

 

 

#include <iostream> using namespace std; template <typename P1,typename P2> class AddT { public: AddT(P1 p1,P2 p2) :m_p1(p1),m_p2(p2){} operator P1() { return m_p1+(P1)m_p2; } operator P2() { return (P2)m_p1+m_p2; } private: const P1 &m_p1; const P2 &m_p2; }; template <typename P1> class AddT<P1,P1> { public: AddT(P1 p1,P1 p2) :m_p1(p1),m_p2(p2){} operator P1() { return m_p1+ (P1)m_p2; } private: const P1 &m_p1,&m_p2; }; template <typename P1,typename P2> AddT<P1,P2> add(const P1& p1,const P2& p2) { return AddT<P1,P2>(p1,p2); } int main() { int a =1; int b = 2; float i = (float)add(a,b); cout<< i <<endl; }

你可能感兴趣的:(Class,float)