Implementation discussion of Singleton class in C ++

zhaozj2021-02-16  83

Singleton in ANSI C is difficult to say, it is easy to say that it is easy, 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.

ID = "AD_TOP" name = "ad_top" align = "left" marginwidth = "0" marginheight = "0" src = "http://adv.pconline.com.cn/adpuba/show?id=pc.rjzx. Kaifa.wenzhang.hzh. & media = HTML & PID = cs.pconline.rjzx.hzh. "FrameBorder =" 0 "width =" 320 "scrolling =" no "height =" 280 ">" design mode "write Singleton to return to pointers :

Class singleton {public: static singleleton * instance (); protected: singleton (); private: static singleleton * _INSTANCE;

The 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 ();

The corresponding Singletonautoptr.cpp is as 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);} CSingletonAutoPtr :: ~ CSingletonAutoPtr () {cout << "CSingletonAutoPtr :: ~ Csingletonautoptr () << end1;} csingletonautoptr * csingletonautoptr :: 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 ();}; corresponding SingleStaticObj. CPP file is: #include "singletonstaticobj.h" #include #include

using namespace std; CSingletonStaticObj CSingletonStaticObj :: m_instance; CSingletonStaticObj :: CSingletonStaticObj () {cout << "CSingletonStaticObj :: CSingletonStaticObj ()" << endl;} CSingletonStaticObj :: ~ CSingletonStaticObj () {cout << "CSingletonStaticObj :: ~ CSingletonStaticObj () "<< Endl;} CSITONSTATICOBJ & CSINGLETONSTATICOBJ :: GetInstance () {Return m_instance;} void csingletonStaticObj :: test () {cout <<" csingletonStaticobj :: test () "<<}

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.

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

New Post(0)