• C++模板类和友元


    12.11 C++模板类和友元

    12.11.1 非模板的友元函数

    12.11.1.1 定义

    friend void report(HasFriend &); // possible?

    这是不可以的,因为形参没有指定类型,应该写为 HasFriend<特定类型(模板参数名或特定数据类型)> & 模板类中的static数据:要求每种模板的实例化都有自己的static成员(每种类型一个)

    12.11.1.2 举例

    HasFriend.h

    #pragma once
    #ifndef HASFRIEND_H_
    #define HASFRIEND_H_
    #include 
    using std::cout;
    using std::endl;
    template 
    class HasFriend
    {
    private:
        T item;
        static int ct;
    public:
        HasFriend(const T& i) : item(i) { ct++; }
        ~HasFriend() { ct--; }
        friend void counts();
        friend void reports(HasFriend&); // template parameter
    };
    // each specialization has its own static data member
    template 
    int HasFriend::ct = 0;//这个就是对于每种类型的初始化
    // non-template friend to all HasFriend classes
    void counts()
    {
        cout << "int count: " << HasFriend::ct << "; ";
        cout << "double count: " << HasFriend::ct << endl;
    }
    // non-template friend to the HasFriend 这两个是重载,类型分别是int和double
    void reports(HasFriend& hf)
    {
        cout << "HasFriend: " << hf.item << endl;
    }
    // non-template friend to the HasFriend class
    void reports(HasFriend& hf)
    {
        cout << "HasFriend: " << hf.item << endl;
    }
    ​
    #endif

    12.11.2 绑定模板友元函数

    12.11.2.1 定义

    定义绑定模板友元函数的三个步骤:

    • 1.在模板类定义之前声明模板函数

    • 2.将模板函数声明为模板类的友元函数

    • 3.为友元函数提供定义

    执行时以友元函数的模板为准,友元函数给啥类型,类模板就是啥类型

    12.11.2.2 举例

    HasFriendT.h

    #pragma once
    #include 
    using std::cout;
    using std::endl;
    // template prototypes
    template  void counts();
    template  void report(T&);
    // template class
    template 
    class HasFriendT
    {
    private:
        TT item;
        static int ct;
    public:
        HasFriendT(const TT& i) : item(i) { ct++; }
        ~HasFriendT() { ct--; }
        friend void counts();
        friend void report<>(HasFriendT&);
    };
    template 
    int HasFriendT::ct = 0;
    // template friend functions definitions
    template 
    void counts()
    {
        cout << "template size: " << sizeof(HasFriendT) << "; ";
        cout << "template counts(): " << HasFriendT::ct << endl;
    }
    template 
    void report(T& hf)
    {
        cout << hf.item << endl;
    }

    12.11.3 非绑定模板友元函数

    12.11.3.1 定义

    友元模板函数的参数和类模板函数的参数不一样。 感觉还是友元函数模板为主导,然后友元函数是啥类型,就决定了使用的类模板类型。

    12.11.3.2 举例

    manyfrnd.h

    #pragma once
    #include 
    using std::cout;
    using std::endl;
    template 
    class ManyFriend
    {
    private:
        T item;
    public:
        ManyFriend(const T& i) : item(i) {}
        template  friend void show2(C&, D&);
    };
    template  void show2(C& c, D& d)
    {
        cout << c.item << ", " << d.item << endl;
    }

    12.11.4 代码

    main.h

    #pragma once
    #ifndef MAIN_H_
    #define MAIN_H_
    #include  //输入输出 
    #include "HasFriend.h" //Template_Classes_and_Friends
    #include "HasFriendT.h" //Template_Classes_and_Friends
    #include "manyfrnd.h" //Template_Classes_and_Friends
    using namespace std;
    void Template_Classes_and_Friends(void)
    {
        std::cout << "\nTemplate_Classes_and_Friends Hello************************************************\n";
            cout << "Non-template friends*********************************************" << std::endl;
            cout << "No objects declared: ";
            counts();
            HasFriend hfi1(10);
            cout << "After hfi1 declared: ";
            counts();
            HasFriend hfi2(20);
            cout << "After hfi2 declared: ";
            counts();
            HasFriend hfdb(10.5);
            cout << "After hfdb declared: ";
            counts();
            reports(hfi1);
            reports(hfi2);
            reports(hfdb);
            cout << "Bound Template Friend Functions***************************************" << std::endl;
            counts();
            HasFriendT hfi3(10);
            HasFriendT hfi4(20);
            HasFriendT hfdb1(10.5);
            report(hfi3); // generate report(HasFriendT &)
            report(hfi4); // generate report(HasFriendT &)
            report(hfdb1); // generate report(HasFriendT &)
            cout << "counts() output:\n";
            counts();
            cout << "counts() output:\n";
            counts();
            cout << "Unbound Template Friend Functions*************************************" << std::endl;
            ManyFriend hfi5(10);
            ManyFriend hfi6(20);
            ManyFriend hfdb2(10.5);
            cout << "hfi5, hfi6: ";
            show2(hfi5, hfi6);
            cout << "hfdb2, hfi6: ";
            show2(hfdb2, hfi6);
            std::cout << "\nTemplate_Classes_and_Friends Bye************************************************\n";
    }
    #endif

    main.cpp

    /*
    Project name :          _12template
    Last modified Date:     2022年5月6日11点33分
    Last Version:           V1.0
    Descriptions:           模板类和友元
    */
    #include "main.h"
    ​
    int main()
    {
        cout << "模板类和友元******************************************************************" << endl;
        Template_Classes_and_Friends();
    ​
        return 0;
    }

    12.11.5 运行结果

    模板类和友元******************************************************************
    Template_Classes_and_Friends Hello************************************************
    Non-template friends*********************************************
    No objects declared: int count: 0; double count: 0
    After hfi1 declared: int count: 1; double count: 0
    After hfi2 declared: int count: 2; double count: 0
    After hfdb declared: int count: 2; double count: 1
    HasFriend: 10
    HasFriend: 20
    HasFriend: 10.5
    Bound Template Friend Functions***************************************
    template size: 4; template counts(): 0
    10
    20
    10.5
    counts() output:
    template size: 4; template counts(): 2
    counts() output:
    template size: 8; template counts(): 1
    Unbound Template Friend Functions*************************************
    hfi5, hfi6: 10, 20
    hfdb2, hfi6: 10.5, 20
    ​
    Template_Classes_and_Friends Bye************************************************
    ​
    D:\Prj\_C++Self\_12template\Debug\_12template.exe (进程 328)已退出,代码为 0。
    要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
    按任意键关闭此窗口. . .
  • 相关阅读:
    AWS认证SAA-C03每日一题
    ssti python 沙箱jinja2利用,PIN获取之[GYCTF2020]FlaskApp
    如何找到redis中的dump.rdb文件?
    【Unity程序技巧】Input管理器
    芯昇XS9950,单通道多合一同轴高清解码芯片方案,一路模拟高清,一路AHD
    Spring 源码(16)Spring Bean的创建过程(7)属性填充
    Mysql事物、隔离级别、锁
    1315. 网格 - 卡特兰数
    python学习之【包和内置模块】
    阿里云k8s环境下,因slb限额导致的发布事故
  • 原文地址:https://blog.csdn.net/weixin_44410704/article/details/127994194