#include#include using namespace std; class person { public: string m_name; int m_age; person(string name,int age) { m_name = name; m_age = age; } }; template 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; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)