C++笔记打卡第16天 (类模版)

1.类模版

  • 建立一个通用类,类中的成员数据类型可以不具体指定,用一个虚拟的类型来替代。
  • 类模版和函数模版语法相似,在声明模版template后面加类,此类称为类模版
// 类模版
template<class NameType, class AgeType>
class Person
{
public:
    Person(NameType name, AgeType age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    NameType m_Name;
    AgeType m_Age;
};

void test01()
{
    Person<string,int> p1("Tom", 18);
}

2.类模版与函数模版的区别

  • 类模版没有自动类型推导的使用方式
  • 类模版在末班参数列表中可以有默认参数
template<class NameType, class AgeType = int>   // 类模版在末班参数列表中可以有默认参数
class Person
{
public:
    Person(NameType name, AgeType age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    void information()
    {
        cout << "姓名:" << this->m_Name
        << " 年龄:" << this->m_Age << endl;
    }
    NameType m_Name;
    AgeType m_Age;
};

void test01()
{
    // Person p1("Tom", 18);  // 错误,类模版没有自动类型推导的使用方式
    Person<string, int> p1("Tom", 18);
    p1.information();
}

void test02()
{
    Person<string> p("Alice", 99);
    p.information();
}

3.类模版中成员函数的创建时机

  • 普通类中的成员函数一开始就可以创建
  • 类模版中的成员函数在调用时才创建
class Person1
{
public:
    void showPerson1()
    {
        cout << "Person1 show" << endl;
    }
};

class Person2
{
public:
    void showPerson2()
    {
        cout << "Person2 show" << endl;
    }
};

template<class T>
class MyClass
{
public:
    T obj;
    // 类模版中的成员函数
    void fun1()
    {
        obj.showPerson1();
    }
    void fun2()
    {
        obj.showPerson2();
    }
};

void test01()
{
    MyClass<Person1> m;  // 正确
    m.fun1();
    m.fun2();  // 错误
}

总结:类模版中的成员函数并不是一开始就创建的,在调用时才会去创建

4.类模版对象做函数参数

  • 类模版实例化的对象,向函数传参的方式
    • 指定传入的类型:直接显示对象的数据类型
    • 参数模版化:将对象中的参数变为模版进行传递
    • 整个类模版化:将这个对象类型模版化进行传递
// 类模版对象做函数参数
template<class T1, class T2>
class Person
{
public:
    Person(T1 name, T2 age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    void show()
    {
        cout << "姓名:" << this->m_name << " 年龄:" << this->m_age << endl;
    }
    T1 m_name;
    T2 m_age;
};

// 1.指定传入类型
void printPerson1(Person<string, int>&p)
{
    p.show();
}

void test01()
{
    Person<string, int> p1("Tom", 18);
    printPerson1(p1);
}

// 2.参数模版化
template<class T1, class T2>
void printPerson2(Person<T1,T2> &p)
{
    p.show();
    cout << "T1的类型:" << typeid(T1).name() << endl;
    cout << "T2的类型:" << typeid(T2).name() << endl;
}

void test02()
{
    Person<string, int> p2("Tom", 19);
    printPerson2(p2);
}

// 3.将整个类模版化
template<class T>
void printPerson3(T &p)
{
    p.show();
    cout << "T的类型:" << typeid(T).name() << endl;
}

void test03()
{
    Person<string, int> p3("Tom", 20);
    printPerson3(p3);
}

5.类模版与继承

  • 当子类继承的父类是一个类模版时,子类在声明的时候,必须指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果要灵活指定出父类中的T的类型,子类也需要变为类模版
template<class T>
class Base
{
public:
    T m;
};

// class Son:public Base 错误,必须知道父类中的T类型,才能继承给子类
class Son:public Base<int>
{

};

void test01()
{
    Son s1;
}

// 如果要灵活指定出父类中的T的类型,子类也需要变为类模版
template<class T1, class T2>
class Son2:public Base<T2>
{
public:
    Son2()
    {
        cout << "T1的类型:" << typeid(T1).name() << endl;
        cout << "T2的类型:" << typeid(T2).name() << endl;
    }  
    T1 obj;
};

void test02()
{
    Son2<int, char>S2;
}

6.类模版成员函数的类外实现

// 类模版成员函数类外实现
template<class T1, class T2>
class Person
{
public:
    Person(T1 name, T2 age);
    void show();
    T1 m_name;
    T2 m_age;
};

// 构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
    this->m_name = name;
    this->m_age = age;
}

// 普通成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::show()
{
    cout << "姓名:" << this->m_name << " 年龄:" << this->m_age << endl;
}

总结:类模版中成员函数类外实现时,需要加上模版参数列表

7.类模版函数分文件编写

类模版中成员函数创建时机是在调用阶段,导致分文件编写时链接不到。

解决方式:

  • 1.直接包含.cpp源文件
  • 2.将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

主函数:

#include<iostream>
#include<string>
#include<person.hpp>
using namespace std;

void test01()
{
    Person<string, int>p("Tom",20);
    p.show();
}

int main()
{
    test01();
    system("pause");
    return 0;
}

person.hpp(声明和实现写在一起):

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

// 类模版成员函数类外实现
template<class T1, class T2>
class Person
{
public:
    Person(T1 name, T2 age);
    void show();
    T1 m_name;
    T2 m_age;
};

// 构造函数的类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
    this->m_name = name;
    this->m_age = age;
}

// 普通成员函数的类外实现
template<class T1, class T2>
void Person<T1, T2>::show()
{
    cout << "name:" << this->m_name << " age:" << this->m_age << endl;
}

8.类模版与友元

  • 全局函数类内实现:直接在类内声明友元即可
  • 全局函数类外实现:需要提前让编译器知道全局函数的存在
// 提前让编译器知道Person类和全局函数的存在
template<class T1, class T2>
class Person;

// 类外实现
template<class T1, class T2>
void printPerson2(Person<T1,T2> p)
{
    cout << "out name:" << p.m_name << " age:" << p.m_age << endl;
}

template<class T1, class T2>
class Person
{
    // 全局函数 类内实现
    friend void printPerson(Person<T1,T2> p)
    {
        cout << "name:" << p.m_name << " age:" << p.m_age << endl;
    }
    // 全局函数 类外实现
    // 加一个空模版的参数列表
    // 如果全局函数类外实现,需要让编译器提前知道这个函数的存在
    friend void printPerson2<>(Person<T1,T2> p);

public:
    Person(T1 name, T2 age)
    {
        this->m_name = name;
        this->m_age = age;
    }
private:
    T1 m_name;
    T2 m_age;
};

void test01()
{
    Person<string,int>p("Tom",30);
    printPerson(p);
}

void test02()
{
    Person<string,int>p2("Tom",60);
    printPerson2(p2);
}

  • 25
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值