首页  

C++模板     所属分类 c 浏览量 697
函数模板  类模板


类模板
template <typename type> ret-type func-name(parameter list){
  // 
}

类模板
template <class type> class class-name {
  //
}



#include <iostream> #include <string> using namespace std; // error: call to 'max' is ambiguous template <typename T> inline T const& getMax (T const& a, T const& b) { return a < b ? b:a; } // template <class T> class Pair { T value1, value2; public: Pair (T first, T second) { value1=first; value2=second; } T getMax (){ // 调用全局函数 加前缀 :: return ::getMax(value1,value2); } }; int main (){ int int1 = 3; int int2 = 7; cout << "getMax(int,int)=" << getMax(int1,int2) << endl; double double1 = 7.1; double double2 = 3.3; cout << "getMax(double,double): " << getMax(double1, double2) << endl; string s1 = "hello"; string s2 = "world"; cout << "getMax(string, string): " << getMax(s1, s2) << endl; Pair<int> intpair(int1,int2); Pair<double> doublepair(double1,double2); Pair<string> strpair(s1,s2); cout << intpair.getMax() << endl; cout << doublepair.getMax() << endl; cout << strpair.getMax() << endl; return 0; }

上一篇     下一篇
各种指针的大小

C语言数据结构笔记

docker VS podman

指针地址输出

指针和内存地址

数据结构数学基础