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;
}
相关推荐
OOJO2 小时前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
会编程的土豆5 小时前
【数据结构与算法】动态规划
数据结构·c++·算法·leetcode·代理模式
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 78. 子集 | C++ 回溯算法题解
c++·算法·leetcode
所以遗憾是什么呢?6 小时前
【题解】Codeforces Round 1081 (Div. 2)
数据结构·c++·算法·acm·icpc·ccpc·xcpc
白藏y7 小时前
【C++】muduo接口补充
开发语言·c++·muduo
xiaoye-duck7 小时前
《算法题讲解指南:递归,搜索与回溯算法--综合练习》--14.找出所有子集的异或总和再求和,15.全排列Ⅱ,16.电话号码的字母组合,17.括号生成
c++·算法·深度优先·回溯
OOJO7 小时前
c++---vector介绍
c语言·开发语言·数据结构·c++·算法·vim·visual studio
Tanecious.8 小时前
蓝桥杯备赛:Day5-P1706 全排列问题
c++·蓝桥杯
胖咕噜的稞达鸭8 小时前
C++技术岗面试经验总结
开发语言·网络·c++·网络协议·tcp/ip·面试
Wild_Pointer.8 小时前
高效工具实战指南:从零开始编写CMakeLists
c++