在cpp中,为什么类模板对象做函数参数时要用引用?(见void func 所在行)
#include<iostream>
using namespace std;
template<class T1,class T2>
class person
{
public:
person(T1 a,T2 b) {
this->m_name = a;
this->m_age = b;
}
void show() {
cout << "姓名:"<<this->m_name <<" "<<"年龄:"<<this->m_age << endl;
}
T1 m_name;
T2 m_age;
};
void func(person <string, int>&p)
p.show();
}
void test1()
{
person <string, int> p1("张三", 20);
func(p1);
}
int main() {
test1();
system("pause");
return 0;
}
少了一个花括号,请见谅