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;
}
相关推荐
lingzhilab16 分钟前
零知派ESP32——BLE Mesh蓝牙组网智能灯控系统(PIR感应+W2812三档调色)
c++·mfc
计算机安禾28 分钟前
【c++面向对象编程】第29篇:定位new(placement new):在指定内存上构造对象
开发语言·c++·算法
计算机安禾33 分钟前
【c++面向对象编程】第27篇:空类的大小为什么是1?——C++对象标识的秘密
开发语言·c++·算法
rGzywSmDg40 分钟前
如何在Dev-C++中选择TDM-GCC编译器
linux·jvm·c++
云泽8081 小时前
笔试算法 - 滑动窗口篇(二):从异位词到最小覆盖子串的通用框架
c++·算法
_wxd6661 小时前
类与对象 (上) (C++)
c++
basketball6161 小时前
并查集基础算法总结 C++ 实现
开发语言·c++·算法
凤凰院凶涛QAQ1 小时前
《C++转Java快速入手系列》String篇:在C++里拼字符串像搬砖,在Java里拼字符串像玩乐高 —— 还是带垃圾回收的那种。
java·开发语言·c++
雪度娃娃2 小时前
Asio——socket的创建和连接
linux·运维·服务器·c++·网络协议