Implementation discussion of Singleton class in C ++

xiaoxiao2021-03-06  114

The implementation of the Singleton class in C discussion of Singleton in ANSI C is difficult to say, it is easy to say, many people write ANSI C Singleton Class has errors. This article discusses how to write Singleton Class in ANSI C , I hope to help everyone.

"Design Mode" is written to return the pointer:

Class singleton {public: static singleleton * instance (); protected: singleton (); private: static singleleton * _instance;}; corresponding implementation CPP file is:

Singleton * Singleton :: _ instance; Singleton * Singleton :: instance () {ix (_instance == 0) {_instance = new singleton;}; return _instance;

The purpose of designing the constructor as protected is to prevent NEW outside of the Class, some people may be designed to be private, if you consider that you may inherit this class, you still need to add a Virtual destructor function. In order to prevent others from copying the Singleton object:

Singleton * psingleton = singleton :: instance (); Singleton S1 = * psingleton; Singleton S2 = * psingleton; the copy constructor function needs to be powered into private. But what exists here is, when do you delete a Singleton object? According to a basic principle of C , where is the object to create where it is destroyed, and you should also put a Destroy method to delete the Singleton object. If you forget to delete it, it is more troublesome. The Instance function also has a multi-threaded lockup problem. If the instance function starts and the end and the end is locked and unlocked, the entire function performance will fall a lot. This is not a good design. There is a small change that avoids forgetting the problem of deleting the Singleton object to bring memory leaks. That is to use STD: Auto_PTR to include a Singleton object, define a Class Static Member Auto_PTR object, automatically delete the Singleton object when the descent static auto_ptr variable is time. In order not to let the user DELETE SINGETON object, you need to turn the designer function to protected by public. The following is the header file singletonautoptr.h:

#include using namespace std; class CSingletonAutoPtr {private: static auto_ptr m_auto_ptr; static CSingletonAutoPtr * m_instance; protected: CSingletonAutoPtr (); CSingletonAutoPtr (const CSingletonAutoPtr &); virtual ~ CSingletonAutoPtr (); // allow auto_ptr to delete , using protected ~ CSingletonAutoPtr () friend class auto_ptr ; public: static CSingletonAutoPtr * GetInstance (); void Test ();}; corresponding SingletonAutoPtr.cpp follows: #include "SingletonAutoPtr.h" #include

// Initial Static Member Vars Here Csingletonautoptr * csingletonautoptr :: M_INSTANCE = NULL; Auto_Ptr csingletonautoptr :: m_auto_ptr;

/// Construction / Destruction / CSingletonAutoPtr :: CSingletonAutoPtr () {cout << "CSingletonAutoPtr :: CSingletonAutoPtr ()" << endl; // put single object into auto_ptr object m_auto_ptr = auto_ptr (this);}

CSITONAUTOPTR :: ~ csingletonautoptr () {cout << "CSITONAUTOPTR :: ~ csingletonautoptr () << endl;}

CSITONAUTOPTR * CSIINGETONAUTOPTR :: getInstance () {// begin lock // ....

IF (m_instance == null) m_instance = new csingletonautoptr ();

// end lock // ...

Return M_INSTANCE;

}

Void csingletonautoptr :: test () {cout << "csingletonautoptr :: test ()" << endl;}

Call method:

CSITONAUTOPTR * psingleton = csingletonautoptr :: getInstance (); psingleton-> test ();

Writing Singleton in a C requires this to be so hard, it is very unexpected. There are many people who have never used Auto_Ptr, and std: auto_ptr it is not perfect, it is based on object ownership mechanism, in contrast, Apache log4cxx has auto_ptr, based on object count, more good. Just use a good auto_ptr to use log4cxx, not very good for many projects. Of course, STD: auto_ptr in ANSI C is enough to write the above example. Another idea is that the GetInstance function is designed to Static Member, because in general, the Singleton object is not large, and the STATIC Member must always occupy memory, the problem is not big. The destructor here must be set to public. The following is the header file SINGESTITICOBJ.H

class CSingletonStaticObj {private: static CSingletonStaticObj m_instance; protected: CSingletonStaticObj (); CSingletonStaticObj (const CSingletonStaticObj &); public: virtual ~ CSingletonStaticObj (); // must public static CSingletonStaticObj & GetInstance (); void Test ();};

The corresponding SingleStaticobj.cpp file is:

#include "singletonstaticobj.h" #include #include

C implementation of the Singleton class discussion Source: 9CBS Editor: ljx [04-7-23 10:16] Author: jacklondon

- · qq 2004 beta version leak! · Summer vacation, bring this Microsoft Encyclopedia home to learn · Microsoft announced the XP SP2 release date will be very late · Battle of browser: GB madon! · Jinshan word 2005 hot trial incompening! · Temptation can not be resistant: Gmail Guide (1) · WinXP SP2 Explore: Watch Safety Center · Large Uses! Are you using tools? · VEGAS5 audio entry: audio processing and plugin · Detailed graphic tutorial: PS hand-painted realistic 蜻 · High difficult airplane game: Yes, the man is top 20 seconds · Classic: Don't say my tears, you don't care, the excitement! QQ 2004 beta version leak! · Summer vacation, bring this Microsoft Encyclopedia to go home to learn · Microsoft announced The XP SP2 release date will be very late · Battle of the browser: GB madthon! · Jinshan word of 2005 hot trial advice! · Temptation cannot be resistant: GMAIL Special (1) · WinXP SP2 Explore: Watch the Safety Center · Large Uses! Are you using a tool? · Vegas5 audio entry: audio processing and plugin · Detailed graphic tutorial: PS hand drawn realistic 蜻 · High difficult airplane game: Yes, a man is top 20 seconds · Classic: Don't say my tears you It doesn't matter! QQ 2004 beta version leak! · Summer vacation, bring this Microsoft Encyclopedia home to learn · Microsoft announced XP SP2 release date will be very late · Battle of browser: GB madon! · Jinshan word 2005 hot trial incoming! · Temptation can not be resistant: Gmail Guide (1) · WinXP SP2 Explore: Watch Safety Center · Large Uses! Are you using tools? · VEGAS5 audio entry: audio processing and plugin · Detailed graphic tutorial: PS hand drawn realistic 蜻 · High difficult airplane game: is a man to top 20 seconds · Classic: Don't say my tears, you don't care

Using namespace std;

CSITONSTATICOBJ :: M_Instance; csingletonstaticobj :: csingletonStaticobj () {cout << "csingletonStaticobj :: csingletonStaticobj ()" <<}

CSITONSTATICOBJ :: ~ cSingletonStaticobj () {cout << "CSITONSTATICOBJ :: ~ csingletonStaticobj () << endl;}

CSITONSTATICOBJ & CSITONSTATICOBJ :: getInstance () {Return m_instance;

Void csingletonStaticobj :: test () {cout << "csingletonStaticobj :: test ()" << Endl;

Call method:

CsingletonStaticobj & Singleton = csingletonautoptr :: getInstance (); singleleton.test ();

From the code quantity, it seems to be easier to use Static Member REF. I am more embarrassed to use this method.

However, not all situations are suitable for STATIC MEMBER SINGETON. For example, GetInstance needs to dynamically decide whether to return different instances, it is not possible. For example, FileSystem :: GetInstance, run under Windows Must return new winfilesystem, Linux / UNIX runs under Windows Must return new LinuxFileSystem, this time you still need to use the above AUTO_PTR to include the Singleton pointer.

转载请注明原文地址:https://www.9cbs.com/read-102476.html

New Post(0)