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;
}
相关推荐
m0_736919108 小时前
C++代码风格检查工具
开发语言·c++·算法
2301_7634724610 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
阿猿收手吧!10 小时前
【C++】std::promise原理与实战解析
c++
m0_7066532311 小时前
分布式系统安全通信
开发语言·c++·算法
Zach_yuan11 小时前
深入浅出 JSONCpp
linux·服务器·网络·c++
寻寻觅觅☆11 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++
lightqjx12 小时前
【C++】unordered系列的封装
开发语言·c++·stl·unordered系列
阿猿收手吧!12 小时前
【C++】string_view:高效字符串处理指南
开发语言·c++
Word码13 小时前
[C++语法] 继承 (用法详解)
java·jvm·c++
lxl130713 小时前
C++算法(1)双指针
开发语言·c++