当前位置: 移动技术网 > IT编程>开发语言>C/C++ > c++类模板之友元函数

c++类模板之友元函数

2019年04月15日  | 移动技术网IT编程  | 我要评论

仲维军,anycall官网,新感觉卡盟

前言:自从开始学模板了后,小编在练习的过程中。常常一编译之后出现几十个错误,而且还是那种看都看不懂那种(此刻只想一句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;
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网