C++ 68 之 类模版作函数的参数

cpp 复制代码
#include <iostream>
// #include <cstring>
#include <string>
using namespace std;

template<class T1, class T2> // 可以设置默认的类型值,后面在使用的时候,就不用再指定类型了
class Students08{
public:
    T1 m_name;
    T2 m_age;
    Students08(const T1 name, T2 age){
        this->m_name = name;
        this->m_age = age;
    }
    void show(){
        cout << "姓名: " << this->m_name << endl;
        cout << "年龄: " << this->m_age << endl;
    }
};

// 1.指定传入类型:使用最广泛
// void sendVal( Students08<string, int > &stu){
//     stu.show();
// }

// 2.参数模板化
template<class T1, class T2>
void sendVal(Students08<T1, T2> &stu)
{
    cout << "T1类型是: " << typeid(T1).name() << endl;
    cout << "T2类型是: " << typeid(T2).name() << endl;
    stu.show();
}

// 3.整个类模版化       T == Students08<string,int>
// template<class T>
// void sendVal(T &stu)
// {
//     // 查看类型的系统函数   typeid(T).name()
//     cout << "T类型是: " << typeid(T).name() << endl;
//     stu.show();
// }


int main()
{
    Students08<string,int> stu1("李四",19);
    sendVal(stu1);
    return 0;
}
相关推荐
Murphy_lx2 小时前
Lambda表达式
开发语言·c++
yangpipi-2 小时前
C++并发编程-23. 线程间切分任务的方法
开发语言·c++
楼田莉子3 小时前
C++算法专题学习——分治
数据结构·c++·学习·算法·leetcode·排序算法
ulias2124 小时前
各种背包问题简述
数据结构·c++·算法·动态规划
程序喵大人4 小时前
分享个C++线程池的实现源码
开发语言·c++·线程池
FL16238631294 小时前
[ubuntu][C++]onnxruntime安装cpu版本后测试代码
linux·c++·ubuntu
要做朋鱼燕5 小时前
【C++】 priority_queue 容器模拟实现解析
开发语言·c++·笔记·职场和发展
励志不掉头发的内向程序员5 小时前
C++进阶——继承 (1)
开发语言·c++·学习
mit6.8247 小时前
并查集|栈
c++
中国胖子风清扬7 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust