• C++成员模板


    12.9 C++成员模板

    12.9.1 定义

    模板可以是结构体、类或模板的成员(例子是beta类)

    12.9.2 代码

    beta.h

    #pragma once
    #ifndef BETA_H_
    #define BETA_H_
    template 
    class beta
    {
    private:
        //这个模板类内部模板类在类内部是这样实现的,也可以在外部实现
        //template  // nested template class member
        //class hold
        //{
        //private:
        //  V val;
        //public:
        //  hold(V v = 0) : val(v) {}
        //  void show() const { std::cout << val << std::endl; }
        //  V Value() const { return val; }
        //};
        //下面这两句是内部类模板的声明
        template  // declaration
        class hold;
        hold q; // template object
        hold n; // template object
    public:
        beta(T t, int i) : q(t), n(i) {}
        //函数模板在模板类内部是这样实现的,也可以在外部实现
        //template // template method
        //U blab(U u, T t) { return (n.Value() + q.Value()) * u / t; }
        //下面这两句是模板函数的声明
        template // declaration
        U blab(U u, T t);
        void Show() const { q.show(); n.show(); }
    };
    ​
    //模板类中模板类在外部的实现
    // member definition
    template 
    template
    class beta::hold
    {
    private:
        V val;
    public:
        hold(V v = 0) : val(v) {}
        void show() const { std::cout << val << std::endl; }
        V Value() const { return val; }
    };
    //模板类中模板函数的外部实现
    // member definition
    template 
    template 
    U beta::blab(U u, T t)
    {
        return (n.Value() + q.Value()) * u / t;
    }
    #endif

    main.h

    #pragma once
    #ifndef MAIN_H_
    #define MAIN_H_
    #include  //输入输出 
    #include "beta.h" //template_member
    ​
    void template_member(void)
    {
        cout << "template_member Hello**********************************************************" << std::endl;
        beta guy(3.5, 3);//T是double类型,T是模板类的成员
        cout << "T was set to double\n";
        guy.Show();
        cout << "V was set to T, which is double, then V was set to int\n";
        cout << guy.blab(10, 2.3) << endl;//U是int类型,因此返回值也为int类型
        cout << "U was set to int\n";
        cout << guy.blab(10.0, 2.3) << endl;//U是double类型,因此返回值也为double类型
        cout << "U was set to double\n";
        std::cout << "template_member 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_member();
        return 0;
    }

    12.9.3 运行结果

    成员模板************************************************************************
    template_member Hello**********************************************************
    T was set to double
    3.5
    3
    V was set to T, which is double, then V was set to int
    28
    U was set to int
    28.2609
    U was set to double
    template_member Bye*********************************************************
    ​
    D:\Prj\_C++Self\_12template\Debug\_12template.exe (进程 11528)已退出,代码为 0。
    要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
    按任意键关闭此窗口. . .

    ##

  • 相关阅读:
    123.Impala查询缓慢问题与解决
    Vue.js 中的异步组件是什么?
    数据库MYSQL
    最详细STM32,cubeMX 定时器
    ELK企业级日志分析系统
    Linux中如何开启一个定时任务
    设计模式-组合模式
    数据可视化学习:Matplotlib概述
    美味的黄油如果不冷藏,会变质吗?
    [第十三篇]——Docker Compose
  • 原文地址:https://blog.csdn.net/weixin_44410704/article/details/127994160