模板的局限性

局限性:模板的通用性并不是万能的
例如判断两个数是否相等的模板,假如传入的是一个Person类,或者一个数组,就会出问题了。
因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板

#include<iostream>
using namespace std;
#include<string>
template<typename T>
bool myCompare(T& a, T& b)
{
    return a == b;
}
void test()
{
    int a = 20, b = 20;
    bool ret = myCompare(a, b);
    if (ret)
    {
        cout << "a==b" << endl;
    }
    else {
        cout << "a!=b" << endl;
    }
}
class Person {
public:
    string name;
    int age;
    Person(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
};
//利用具体化Person的版本实现代码,会优先调用这个。
template<> bool myCompare(Person& p1, Person& p2)
{
    if (p1.name == p2.name && p1.age == p2.age)
    {
        return true;
    }
    else {
        return false;
    }

}
void test01()
{
    Person p1("tom", 18);
    Person p2("tom", 16);
    bool ret = myCompare(p1, p2);
    if (ret)
    {
        cout << "p1==p2" << endl;
    }
    else {
        cout << "p1!=p2" << endl;
    }
}
int main()
{
    //test();
    test01();
}

总结:

  • 利用具体化的模板,可以解决自定义类型的通用化
  • 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板

类模板的基本语法

类模板作用:建立一个通用类,类中的成员数据类型可以不具体制定,用一个虚拟的类型来代表。
语法:
template <typename T>

解释:

  • template:声明创建模板
  • typename:表面其后面的符号是一种数据类型,可以用class代替
  • T通用的数据类型,名称可以替换,通常为大写字母
#include<iostream>
using namespace std;
#include<string>
template<class T1, class T2>
class Person
{
public:
    Person(T1 name, T2 age)
    {
        this->name = name;
        this->age = age;
    }
    T1 name;
    T2 age;
    void show()
    {
        cout << name << endl;
        cout << age << endl;
    }
};
void test()
{
    Person<string, int> p1("张三", 20);
    p1.show();
}
int main()
{
    test();
}

总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板

类模板

类模板与函数模板区别主要有两点:

  • 类模板没有自动类型推导的使用方式(c++17之后有了)
  • 类模板在模板参数列表中可以有默认参数
#include<iostream>
using namespace std;
#include<string>
//模板也可以指定默认参数
template<class T1 = string, class T2 = int>
class Person
{
public:
    T1 name;
    T2 age;
    Person(T1 name, T2 age)
    {
        this->name = name;
        this->age = age;
    }
    void show()
    {
        cout << name << endl;
        cout << age << endl;

    }
};
void test1()
{
    Person p1("張三", 18);
    Person p2("李四", 20);
    p1.show();
    p2.show();
}
void test2()
{
    Person p("tom", 20);
    p.show();
}
int main()
{
    //test1();
    test2();
}

总结:

  • 类模板使用只能用显示指定类型方式(c++17之后有了自动类型推导,可以不指定了)
  • 类模板中的模板参数列表可以有默认参数

类模板中成员函数的创建时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:

  • 普通类中的成员函数一开始就可以创建
  • 类模板中的成员函数在调用时才创建

    #include<iostream>
    using namespace std;
    #include<string>
    class Person1 {
    public:
      void showPerson1()
      {
          cout << "Person 1 show" << endl;
      }
    };
    class Person2 {
    public:
      void showPerson2()
      {
          cout << "Person 2 show" << endl;
      }
    };
    template<class T>
    class MyClass {
    public:
      T obj;
      void func1()
      {
          obj.showPerson1();
      }
      void func2()
      {
          obj.showPerson2();
      }
    };
    void test01()
    {
      MyClass<Person1> m;
      m.func1();
      //m.func2();报错,Perosn1不能调用Person2的showPerson2()。
    }
    
    int main()
    {
      test01();
    }

    总结:类模板中的成员函数并不是一开始就创建的,在调用时才去创建.因为使用了模板,我们无法确定obj的类型。

类模板对象做函数参数

一共有三种传入方式:

  1. 指定传入的类型,直接显示对象的数据类型
  2. 参数模板化,将对象中的参数变为模板进行传递
  3. 整个类模板化 将这个对象类型模板化进行传递
#include<iostream>
using namespace std;
template<class T1, class T2>
class Person {
public:
    T1 name;
    T2 age;
    Person(T1 name, T2 age)
    {
        this->age = age;
        this->name = name;
    }
    void show()
    {
        cout << "姓名" << name << endl;
        cout << "年龄" << age << endl;
    }
};
//第一种方式,指定传入类型。
void printPerson1(Person<string, int>& p)
{
    cout << "-------------------" << endl;
    p.show();
}
//第二种方式,参数模板化
template<class T1, class T2>
void printPerson2(Person<T1, T2>& p)
{
    cout << "-------------------" << endl;
    p.show();
    cout << "T1的类型为:" << typeid(T1).name() << endl;
    cout << "T2的类型为:" << typeid(T2).name() << endl;
    //想看模板的推导的类型可以使用typeid(T).name()的方式查看。
}
//第三种方式,整个类模板化
template <class T>
void printPerson3(T& p)
{
    cout << "-------------------" << endl;
    p.show();
    cout << "T的类型为" << typeid(T).name() << endl;
}
void test()
{
    Person<string, int> p1("tom", 18);
    printPerson1(p1);
    printPerson2(p1);
    printPerson3(p1);
}
int main()
{
    test();
}

总结:

  • 通过类模板创建的对象,可以有三种方式向函数中进行传参
  • 使用比较广泛是第一种:指定传入的类型,第二种和第三种就有一点绕弯了。

类模板与继承

  • 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需变为类模板
#include<iostream>
using namespace std;
template<class T>
class Base {
public:
    T m;
};
//class Son :public Base //报错,必须指定父类中的T类型,才能继承给子类
class Son1 :public Base<int>
{
};
//如果想灵活指定父类中T类型,子类也需要变为类模板
template<class T1, class T2>
class Son2 :public Base<T1> {
public:
    T2 obj;
    Son2()
    {
        cout << "T1的类型:" << typeid(T1).name() << endl;
        cout << "T2的类型:" << typeid(T2).name() << endl;
    }
};
void test()
{
    Son1 s1;
    Son2<int, char> s2;
}
int main()
{
    test();
}

总结:如果父类是类模板,子类需要指定出父类中T的数据类型

类模板成员函数类外实现

#include<iostream>
using namespace std;
#include<string>
template <class T1, class T2>
class Person
{
public:
    T1 name;
    T2 age;
    Person(T1 name, T2 age);
    //{
    //    this->name = name;
    //    this->age = age;
    //}
    void show();
    //{
    //    cout << "姓名:" << name << endl;
    //    cout << "年龄:" << age << endl;
    //}
};
//构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
    this->name = name;
    this->age = age;
}
//成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::show()
{
    cout << "姓名:" << name << endl;
    cout << "年龄:" << age << endl;
}
void test()
{
    Person<string, int> p("tom", 18);
    p.show();
}
int main()
{
    test();
}

总结:类模板中成员函数类外实现时,和之前普通函数类外实现类似,需要加上Person作用域,但是还需要加上模板参数列表

类模板分文件编写

问题:

  • 类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到解决
  • 解决方式1:直接包含.cpp源文件
  • 解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

方案一:

//主代码文件,文件名为main.cpp
#include<iostream>
using namespace std;
#include<string>
#include "Person.hpp"
void test()
{
    Person<string,int> p("tom",18);
    p.show();
}
int main()
{
    test();
}
//hpp文件,类的声明和实现都在这一个文件,文件名为Person.hpp
#pragma once
#include<iostream>
using namespace std;
#include<string>
template<class T1, class T2>
class Person {
public:
    T1 name;
    T2 age;
    Person(T1 name, T2 age);
    void show();
};
template<class T1, class  T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
    this->name = name;
    this->age = age;
}
template<class T1, class  T2>
void Person<T1, T2>::show()
{
    cout << "姓名:" << name << endl;
    cout << "年龄" << age << endl;
}

方案二

//主代码文件,文件名为main.cpp
#include<iostream>
using namespace std;
#include<string>
#include "Person.cpp"
void test()
{
    Person<string, int> p("tom", 18);
    p.show();
}
int main()
{
    test();
}
//Person头文件,文件名为Person.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
template<class T1, class T2>
class Person {
public:
    T1 name;
    T2 age;
    Person(T1 name, T2 age);
    void show();
};
//Person实现文件,文件名为Person.cpp
#include "Person.h"
template<class T1, class  T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
    this->name = name;
    this->age = age;
}
template<class T1, class  T2>
void Person<T1, T2>::show()
{
    cout << "姓名:" << name << endl;
    cout << "年龄" << age << endl;
}

类模板与友元

全局函数类内实现:直接在类内声明友元即可
全局函数类外实现:需要提前让编译器知道全局函数的存在

#include<iostream>
using namespace std;
#include<string>
template<class T1, class T2>
class Person;
//全局函数类外实现
template<class T1, class T2>
void printPerson2(Person<T1, T2> p)
{
    cout << "姓名:" << p.name << endl;
    cout << "年龄:" << p.age << endl;
}
template<class T1, class T2>
class Person
{
    //全局函数类内实现
    friend void printPerson(Person<T1, T2>p)
    {
        cout << "姓名:" << p.name << endl;
        cout << "年龄:" << p.age << endl;
    }
    //如果全局函数是类外实现的,需要让编译器提前知道这个函数的存在,但是这个函数又不知道Person类的存在,所以又要先定义Person,才能正常编译。
    friend void printPerson2<>(Person<T1, T2> p);
public:
    Person(T1 name, T2 age)
    {
        this->name = name;
        this->age = age;
    }
private:
    T1 name;
    T2 age;
};

void test()
{
    Person p("tom", 18);
    printPerson(p);
    printPerson2(p);
}
int main()
{
    test();
}

总结:建议全局函数做类内实现,用法简单,而且编译器可以直接识别,类外实现过于复杂。