zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    性能测试七:jmeter进阶之文件上传下载、定时器
    性能测试六:jmeter进阶之Cookie与header管理器
    导出文本、表格、图像到PDF格式文件中(学习整理)
    数据库(学习整理)----7--Oracle多表查询,三种join连接
    反射(学习整理)----Class类和加载器ClassLoader类的整理
    反射(学习整理)----操作类、属性、方法!
    数据库(学习整理)----6--Oracle如何快速备份和多次备份数表数据
    数据库(学习整理)----5--Oracle常用的组函数
    数据库(学习整理)----4--Oracle数据查询(基础点1)
    Java小例子(学习整理)-----学生管理系统-控制台版
  • 原文地址:https://www.cnblogs.com/lovemee/p/10706402.html
Copyright © 2011-2022 走看看