0
点赞
收藏
分享

微信扫一扫

typeid(T1).name()告诉模板T的数据类型

DYBOY 2022-04-14 阅读 47
c++

#include<iostream>
#include<string>
using namespace std;
//全局函数的类内实现与类外实现
template<typename T1, typename T2>
class person;
template<typename T1, typename T2>
void showperson2(person<T1, T2> p) {
    cout << "全局函数做友元类外实现" << endl;
    cout << "T1的数据类型为" << typeid(T1).name() << endl;
    cout << "T2的数据类型为" << typeid(T2).name() << endl;
    cout << "姓名为:" << p.name << endl;
    cout << "年龄为:" << p.age << endl;
}
template<typename T1,typename T2>
class person {
public:
    friend void showperson2<>(person<T1, T2> p);
    T2 name;
T1 age;
    person(T1 age,T2 name) {
        this->age = age;
        this->name = name;
    }
    friend void showperson(person<T1,T2> p) {
        cout << "全局函数做友元类内实现" << endl;
        cout <<"T1的数据类型为" << typeid(T1).name() << endl;
        cout << "T2的数据类型为" << typeid(T2).name() << endl;
        cout << "姓名为:" << p.name << endl;
        cout << "年龄为:" << p.age << endl;
    }
    
};

void teest01() {
    person<int,string> p(18, "王二狗");
    showperson( p);
}
void test02() {
    person<int, string> p(56, "李云龙");
    showperson2<>(p);
}


int main() {
    teest01();
    test02();
    return 0;
}

举报

相关推荐

0 条评论