C++ 类模板对象做函数参数

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

template<class T1,class T2>

class Person

{

public:

Person(T1 name,T2 age)

{

this->name=name;

this->age=age;

}

void showPerson()

{

cout<<"name:"<<this->name<<endl;

cout<<"age:"<<this->age<<endl;

}

private:

T1 name;

T2 age;

};

void printPerson1(Person<string,int> &p)

{

p.showPerson();

}

template<class T1,class T2>

void printPerson2(Person<T1,T2> &p)

{

p.showPerson();

cout<<":"<<typeid(T1).name()<<endl;

cout<<":"<<typeid(T2).name()<<endl;

}

template<class T>

void printPerson3(T &p)

{

p.showPerson();

cout<<":"<<typeid(T).name()<<endl;

}

void test03()

{

Person<string,int>p("Tom",10);

printPerson3(p);

}

void test01()

{

Person<string,int>p("Tom",10);

printPerson2(p);

}

int main()

{

test03();

system("pause");

}

相关推荐
王老师青少年编程2 小时前
csp信奥赛C++标准模板库STL案例应用1
c++·算法·stl·标准模板库·csp·信奥赛·binary_search
最贪吃的虎2 小时前
网络是怎么传输的:从底层协议到浏览器访问网站的全过程剖析
java·开发语言·网络·http·缓存
云栖梦泽2 小时前
鸿蒙应用全流程上线实战:从合规到运营的闭环落地
开发语言·鸿蒙系统
大猫会长2 小时前
react中用css加载背景图的2种情况
开发语言·前端·javascript
NAGNIP2 小时前
Kimi Linear——有望替代全注意力的全新注意力架构
算法·面试
篱笆院的狗2 小时前
Java 中线程之间如何进行通信?
java·开发语言
坐吃山猪2 小时前
Python命令行工具argparse
开发语言·python
lsx2024062 小时前
jQuery 密码验证
开发语言
创作者mateo2 小时前
python进阶之文件处理
开发语言·python