c++类模板之友元函数

前言:自从开始学模板了后,小编在练习的过程中。常常一编译之后出现几十个错误,而且还是那种看都看不懂那种(此刻只想一句MMP)。于是写了便写了类模板友元函数的用法这篇博客。来记录一下自己的学习。

普通友元函数的写法:

第一种:(直接上代码吧)

#include <iostream>
#include <string>

using namespace std;

template<class T>
class Person{
public:
    Person(T n)
    {
        cout << "Person" << endl;
        this->name = n;
    }
    ~Person()
    {
        cout << "析构函数" << endl;
    }
    //友元函数
    /********************************/
    template<class T>
    friend void print(Person<T> &p);
    /*******************************/
private:
    T name;
};

//友元函数
template<class T>
void print(Person<T> &p)
{
    cout << p.name << endl;
}



int main()
{
    Person<string>P("XiaoMing");
    print(P);

    system("pause");
    return 0;
}

 第二种方法:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 //方法二必不可缺的一部分
 7 /**************************************/
 8 template<class T> class Person;
 9 template<class T> void print(Person<T> &p);
10 /****************************************/
11 template<class T>
12 class Person{
13 public:
14     Person(T n)
15     {
16         cout << "Person" << endl;
17         this->name = n;
18     }
19     ~Person()
20     {
21         cout << "析构函数" << endl;
22     }
23     //友元函数
24     /********************************/
25     friend void print<T>(Person<T> &p);
26     /*******************************/
27 private:
28     T name;
29 };
30 
31 //友元函数
32 template<class T>
33 void print(Person<T> &p)
34 {
35     cout << p.name << endl;
36 }
37 
38 
39 
40 int main()
41 {
42     Person<string>P("XiaoMing");
43     print(P);
44 
45     system("pause");
46     return 0;
47 }

运算符重载中的友元函数:

方法一:

#include <iostream>
#include <string>

using namespace std;


template<class T>
class Person{
public:
    Person(T n)
    {
        cout << "Person" << endl;
        this->name = n;
    }
    ~Person()
    {
        cout << "析构函数" << endl;
    }
    //友元函数
    /********************************/
    template<class T>
    friend ostream& operator<<(ostream &os, Person<T> &p);
    /*******************************/
private:
    T name;
};

//运算符重载
template<class T>
ostream& operator<<(ostream &os, Person<T> &p)
{
    os << p.name << endl;
    return os;
}


int main()
{
    Person<string>P("XiaoMing");
    cout << P << endl;
    system("pause");
    return 0;
}

方法二:

#include <iostream>
#include <string>

using namespace std;


template<class T>
class Person{
public:
    Person(T n)
    {
        cout << "Person" << endl;
        this->name = n;
    }
    ~Person()
    {
        cout << "析构函数" << endl;
    }
    //友元函数
    /********************************/
    //template<class T>
    friend ostream& operator<<<T>(ostream &os, Person<T> &p);
    /*******************************/
private:
    T name;
};

//运算符重载
template<class T>
ostream& operator<<(ostream &os, Person<T> &p)
{
    os << p.name << endl;
    return os;
}


int main()
{
    Person<string>P("XiaoMing");
    cout << P << endl;
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lovemee/p/10706402.html