我是靠谱客的博主 刻苦曲奇,这篇文章主要介绍类模板的对象作为函数参数传入三种方式及其的应用案例 c++ 简单易懂,现在分享给大家,希望可以做个参考。

1.指定化类型的方式进行传入,一般都是按照这种方式进行传入的
2.将参数模板化
3.将整个类模板化
应用案例如下:

#include<iostream>
using namespace std;
template<typename nametype,typename agetype>
class person
{
public: 
	person(nametype name, agetype age)
{
	this->m_age = age;
	this->m_name = name;
} 
	void showinfor()
	{
		cout << "姓名" << m_name << "年龄" << m_age << endl;
	}
	  nametype m_name;
	  agetype m_age;
};
void myprint1(person < string, int>&p)
{//指定化类型的方式进行传入,一般都是按照这种方式
	//进行传入的
	cout << "姓名:" << p.m_name << "年龄" << p.m_age << endl;
}
template<typename T1,typename T2>
void myprint2(person<T1, T2>& p)
{//将参数模板化
	cout << "姓名:" << p.m_name << "年龄" << p.m_age << endl;
}
template<typename T>
void myprint3(T&p)
{//将整个类模板化
	cout << "姓名:" << p.m_name << "年龄" << p.m_age << endl;
}
void test01()
{
	person<string, int>p("tom", 18);
	myprint1(p);
}
void test02()
{
	person<string, int>p("tom", 18);
	myprint2(p);
}
void test03()
{
	person<string, int>p("tom", 18);
	myprint3(p);
}
int main(void)
{
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

最后

以上就是刻苦曲奇最近收集整理的关于类模板的对象作为函数参数传入三种方式及其的应用案例 c++ 简单易懂的全部内容,更多相关类模板的对象作为函数参数传入三种方式及其的应用案例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(115)

评论列表共有 0 条评论

立即
投稿
返回
顶部