C++实现单例模式的一个实例

[复制链接]
 楼主| keer_zu 发表于 2022-10-9 16:49 | 显示全部楼层 |阅读模式
代码:
  1. #ifndef __IPERF_COLLECTION_H__
  2. #define __IPERF_COLLECTION_H__

  3. #include <map>
  4. #include <pthread.h>
  5. #include "macros.h"

  6. using namespace std;

  7. typedef void ( * port_performance_calback_t ) ( void * data ) ;

  8. template<typename T>
  9. class Singleton{
  10. public:
  11.     static T& get_instance(){
  12.         static T instance;
  13.         return instance;
  14.     }
  15.        
  16.     virtual ~Singleton(){
  17.         std::cout<<"destructor called!"<<std::endl;
  18.     }
  19.        
  20.     Singleton(const Singleton&)=delete;
  21.     Singleton& operator =(const Singleton&)=delete;
  22.        
  23. protected:
  24.     Singleton(){
  25.         std::cout<<"constructor called!"<<std::endl;
  26.     }

  27. };


  28. class IperfCollection:public       Singleton<IperfCollection> {
  29.         friend class Singleton<IperfCollection>;
  30. public:

  31.         int Init(port_performance_calback_t cb);
  32.        
  33. private:

  34.         int createFifo();
  35.         int stringParsing(string str,int port);
  36.         static void *iperfDataCollection(void *data);
  37.         IperfCollection():_callback(NULL){}
  38.         ~IperfCollection(){}
  39.        
  40.         pthread_t        _threadId;
  41.         port_performance_calback_t _callback;
  42.        
  43.         map<int,string> _portFifo;
  44.         map<int,int> _portFd;
  45. };


  46. #endif


 楼主| keer_zu 发表于 2022-10-9 16:50 | 显示全部楼层
里面:
  1. Singleton(const Singleton&)=delete;
  2. Singleton& operator =(const Singleton&)=delete;


这部分表明禁止拷贝
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1473

主题

12899

帖子

55

粉丝
快速回复 返回顶部 返回列表