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;
}
相关推荐
利刃大大31 分钟前
【动态规划:背包问题】完全平方数
c++·算法·动态规划·背包问题·完全背包
笑非不退1 小时前
C# c++ 实现程序开机自启动
开发语言·c++·c#
AA陈超2 小时前
从0开始学习 **Lyra Starter Game** 项目
c++·笔记·学习·游戏·ue5·lyra
q***T5832 小时前
C++在游戏中的Unreal Engine
c++·游戏·虚幻
保持低旋律节奏2 小时前
C++——C++11特性
开发语言·c++·windows
小张成长计划..4 小时前
【C++】16:模板进阶
c++·算法
CoderIsArt4 小时前
SAM-5 核心类体系的 C++ 完整设计
c++·sam5
CS_浮鱼4 小时前
【Linux进阶】mmap实战:文件映射、进程通信与LRU缓存
linux·运维·c++·缓存
YJlio5 小时前
「C++ 40 周年」:从“野蛮生长的指针地狱”到 AI 时代的系统底座
c++·人工智能·oracle
纵有疾風起5 小时前
C++——多态
开发语言·c++·经验分享·面试·开源