Singleton模式

来源:互联网 发布:中国频道域名转出费用 编辑:程序博客网 时间:2024/05/16 12:34

 1. Introduction

 

  This design pattern and methodology ensures that only one instance of the C++ class is instantiated. It assures that only one object is created and no more. It is often used for a logging class so only one object has access to log files, or when there is a single resource, where there should only be a single object in charge of accessing the single resource. The singleton pattern discussed here gives the class itself, the responsibility of enforcement of the guarantee that only one instance of the class will be allowed to be generated.

 

2. example

   singleton.h 代码如下:

 

 

singleton.cpp 代码如下:

 

client.cpp 代码如下

 

运行结果如下:

原创粉丝点击