#include <iostream>
template<class typeName, class typeAge>
class Person
{
public:
Person(typeName name, typeAge age):
name(name),
age(age)
{}
void show()
{
std::cout << "name " << name << std::endl;
std::cout << "age " << age << std::endl;
}
protected:
typeName name;
typeAge age;
};
int main()
{
/* 语法错误 */
//Person p("furong", 10);
Person<std::string, int> p("furong", 10);
p.show();
return 0;
}
$ ./a.out
name furong
age 10
注:类模板只能显示指定类型方式