模板具体化的实例

#include <iostream>
#include<string>
using namespace std;

class Person
{
public:
    int m_age;
    string m_name;
    Person(int age, string name)
    {
        m_age = age;
        m_name = name;
    }
};

template<class T>
bool isEqual(T a, T b)
{
    if (a == b)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//具体化模板实现类的比较功能
template<> bool isEqual(Person p1, Person p2)
{
    if (p1.m_name == p2.m_name && p1.m_age == p2.m_age)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void test01()
{
    Person p1(10, "Tom");
    Person p2(20, "Tom");
    cout<< isEqual(p1, p2);
}

int main()
{
    test01();
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42817985/article/details/117125988