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++ 简单易懂的全部内容,更多相关类模板的对象作为函数参数传入三种方式及其的应用案例内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复