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");

}

相关推荐
冉佳驹4 分钟前
C++11 ——— 列表初始化、移动语义、可变参数模板、lamdba表达式、function包装器和bind包装器
c++·可变参数模板·移动构造·移动赋值·function包装器·bind包装器·lamdba表达式
leaves falling13 分钟前
c语言单链表
c语言·开发语言
xu_yule15 分钟前
算法基础—组合数学
c++·算法
爱尔兰极光16 分钟前
LeetCode--移除元素
算法·leetcode·职场和发展
独自破碎E16 分钟前
【中心扩展法】LCR_020_回文子串
java·开发语言
XLYcmy19 分钟前
一个用于统计文本文件行数的Python实用工具脚本
开发语言·数据结构·windows·python·开发工具·数据处理·源代码
4311媒体网26 分钟前
自动收藏功能的实现方法
java·开发语言
xyq202428 分钟前
SQLite 创建表
开发语言
Tansmjs28 分钟前
C++中的工厂模式变体
开发语言·c++·算法