您的位置:首页 > 编程语言 > C语言/C++

C++ 函数模板和模板类

2016-09-13 09:55 281 查看
#include <QCoreApplication>


#include <iostream>


using namespace std;


//函数模板

template<typename T>

bool equivalent(const T&a, const T&b)

{

return !(a<b)&& !(b<a);

}


//类模板

template<typename T=int>//默认参数

class Bignumber{

typedef T value_type;


public:

Bignumber(value_type a):

m_v(a)

{


}


inline bool operator <(const Bignumber &obj)const

{

return m_v< obj.m_v;

}


private:

value_type m_v;

};



int main(int argc, char *argv[])

{

QCoreApplication a(argc, argv);


Bignumber<> x(1), b(1);


bool ret = equivalent(x,b);//函数模板自动推导


cout<<ret<<endl;


cout<<equivalent<double>(5,1);//函数模板特化

while (1) {


}


return a.exec();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: