c++之类模板成员函数的类外实现


#include<iostream>
using namespace std;

template<class T1,class T2>
class Person {
public:
    Person(T1 name,T2 age);
    void show();
    T1 name;
    T2 age;
};

template<class T1,class T2>
Person<T1,T2>::Person(T1 name, T2 age) {
    this->name = name;
    this->age = age;
}
//对于成员函数,需要指明类的参数的代表
template<class T1, class T2>
void Person<T1, T2>::show() {
    cout << this->name << endl;
    cout << this->age << endl;
}

void test() {
    Person<string, int> p("tom",12);
    p.show();
}

int main() {
    test();
    system("pause");
    return 0;
}

输出:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM