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;
}
相关推荐
Sheep Shaun16 分钟前
C++11核心特性详解:从右值引用到现代C++编程
开发语言·数据结构·c++·算法
小王努力学编程26 分钟前
LangChain——AI应用开发框架
服务器·c++·人工智能·分布式·rpc·langchain·brpc
呱呱巨基30 分钟前
Linux Ext系列文件系统
linux·c++·笔记·学习
云深麋鹿31 分钟前
三.栈和队列
开发语言·数据结构·c++·算法
爆打维c37 分钟前
01BFS算法(例题:网格传送门旅游)
c语言·c++·python·算法·leetcode·广度优先
像素猎人44 分钟前
力扣:面试题16.01.交换数字
c++·算法·leetcode·面试
CSDN_RTKLIB1 小时前
C++仿函数
c++·算法·stl
沉默-_-2 小时前
力扣hot100双指针专题解析2(C++)
java·c++·算法·蓝桥杯·双指针
福楠2 小时前
C++ | 红黑树
c语言·开发语言·数据结构·c++·算法
Trouvaille ~2 小时前
【Linux】进程间通信(三):共享内存深度剖析与System V IPC机制
linux·c++·操作系统·管道·进程间通信·信号量·system v