当前位置:   article > 正文

C++ 成员模板类

C++ 成员模板类
#include <iostream>         // 包含头文件。
using namespace std;        // 指定缺省的命名空间。

template<class T1, class T2>
class AA              // 类模板AA。
{
public:
    T1 m_x;
    T2 m_y;

    AA(const T1 x, const T2 y) : m_x(x), m_y(y) {}
    void show() { cout << "m_x=" << m_x << ",m_y=" << m_y << endl; }

    template<class T>
    class BB
    {
    public:
        T m_a;
        T1 m_b;
        BB() {}
        void show();
    };
    BB<string> m_bb;

    template<typename T>
    void show(T tt);
};

template<class T1, class T2>
template<class T>
void AA<T1,T2>::BB<T>::show() { 
    cout << "m_a=" << m_a << ",m_b=" << m_b << endl; 
}

template<class T1, class T2>
template<typename T>
void AA<T1,T2>::show(T tt) {
    cout << "tt=" << tt << endl;
    cout << "m_x=" << m_x << ",m_y=" << m_y << endl;
    m_bb.show();
}

int main()
{
    AA<int, string> a(88, "我是一只傻傻鸟。");
    a.show();
    a.m_bb.m_a = "我有一只小小鸟。";
    a.m_bb.show();
    a.show("你是一只什么鸟?");
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
https://xxetb.xet.tech/s/3Zqhgt

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/777974
推荐阅读
相关标签
  

闽ICP备14008679号