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;
}
相关推荐
Stanford_11061 天前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
千里马-horse1 天前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Lucis__1 天前
再探类&对象——C++入门进阶
开发语言·c++
北京不会遇到西雅图1 天前
【SLAM】【后端优化】不同优化方法对比
c++·机器人
jndingxin1 天前
c++多线程(6)------ 条件变量
开发语言·c++
程序员莫小特1 天前
老题新解|大整数加法
数据结构·c++·算法
洲覆1 天前
C++ 模板、泛型与 auto 关键字
开发语言·数据结构·c++
千里马-horse1 天前
Async++ 源码分析7--parallel_reduce.h
开发语言·c++·async++·parallel_reduce
江公望1 天前
Qt QThread使用方法入门浅解
c++·qt
叫我龙翔1 天前
【MySQL】从零开始了解数据库开发 --- 数据表的约束
android·c++·mysql·数据库开发