zoukankan      html  css  js  c++  java
  • c/c++ 模板与STL小例子系列<二> 模板类与友元函数

    c/c++ 模板与STL小例子系列<二> 模板类与友元函数

    比如某个类是个模板类D,有个需求是需要重载D的operator<<函数,这时就需要用到友元。

    实现这样的友元需要3个必要步骤

    1,在模板类D的实现代码的上面声明友元函数

    template<typename> class D;//因为友元函数的参数里使用了D,所以要先在这里声明一下
    template<typename T>
    ostream& operator<< (ostream&, const D<T> &);
    

    2,在模板类D的实现代码里面声明它是我的友元

    //注意operator<<后面有<T>                                                  
    friend ostream& operator<< <T>(ostream& ,const D<T>&);
    

    3,实现友元函数

    template<typename T>
    //注意operator<<后面没有<T> 
    ostream& operator << (ostream& out,const D<T>& d){
      out << d.x;
      return out;
    }
    

    例子代码:

    #include <iostream>
    using namespace std;
    
    template<typename T>
    class Test{
    public:
      Test(T t) : data(t){}
      virtual void show() = 0;
    private:
      T data;
    };
    
    template<typename> class D;
    template<typename T>
    ostream& operator<< (ostream&, const D<T> &);
    
    template<typename T>
    class D : public Test<T>{
      //注意有<T>                                                  
      friend ostream& operator<< <T>(ostream& ,const D<T>&);
    public:
    
      //注意不是Test(t1)                                           
      D(T t1, T t2) : Test<T>(t1), x(t2){}
      void show(){
        cout << x << ", " << x << endl;
      }
    private:
      T x;
    };
    
    template<typename T>
    ostream& operator << (ostream& out,const D<T>& d){
      out << d.x;
      return out;
    }
    
    int main(void){
      Test<int> *p = new D<int>(10, 21);
      p->show();
      D<int> d(10,20);
      cout << d << endl;
      return 0;
    }
    

    模板类继承非模板类,非模板类继承模板类

    下面的例子没有什么实际意义,只看语法。

    #include <iostream>
    using namespace std;
    
    class Foo{
    public:
      Foo(int a, int b, int c) : x(a), y(b), z(c){}
      void show(){
        cout << x << "," << y << "," << z << endl;
      }
    private:
      int x, y, z;
    };
    
    template <typename T>
    class Goo : public Foo{
    public:
      Goo(T t, int a, int b, int c):Foo(a,b,c), data(t){}
      void show(){
        cout << data << endl;
        cout << "Goo show" << endl;
      }
    private:
      T data;
    };
    
    class Hoo : public Goo<int>{
    public:
      Hoo(int a1,int a2,int a3,int a4,int a5):
        Goo(a1,a2,a3,a4),ho(a5){}
      void show(){
        cout << "Hoo show" << endl;
      }
    private:
      int ho;
    };
    int main(void){
      Hoo hoo(1,2,3,4,5);
      hoo.show();
      Goo<string> goo("abc",1,2,3);
      goo.show();
      return 0;
    }
    
    
  • 相关阅读:
    Static Analysis of Java Enterprise Applications: Frameworks and Caches, the Elephants in the Room
    使用gopacket 解析一个简单的sql server 协议
    gopacket 流量抓包golang 包
    pmm 自定义prometheus 配置
    Hasura 即将支持更多的数据库类型
    apache knox apache hdaoop 生态的rest api && 应用网关
    使用minio替换fastdfs 文件系统
    tcpcollect基于libpcap 监听mysql sql
    一种业务延时状态分析的方法
    tcprstat 使用的几个问题
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9551368.html
Copyright © 2011-2022 走看看