#include<iostream>
#include<string>
using namespace std;
class person {
public:
string m_name;
int m_age;
person(string name,int age) {
m_name = name;
m_age = age;
}
};
template<class T>
bool mycompare(T& a, T& b) {
if (a == b)
return true;
else
return false;
}
//利用具体化 具体化会优先调用 可以解决自定义类型的通用化
template<> bool mycompare(person &p1, person &p2) {
if (p1.m_age == p2.m_age && p1.m_name == p2.m_name)
return true;
else
return false;
}
void test1() {
person a("tom",10);
person b("tom",10);
bool ret = mycompare(a, b);
if (ret == true)
{
cout << "a=b" << endl;
}
else
cout << "a!=b" << endl;
}
int main() {
test1();
system("pause");
return 0;
}
c++利用模板的具体化解决自定义类型的通用化
最新推荐文章于 2024-11-17 16:56:14 发布