C ++ realizes a single piece

zhaozj2021-02-08  268

There is a mode called a single piece (SIGLETON) mode in Design Mode, which is used to control the creation of a unique object. Only how to establish a Singleton object in the book, only words on how to destroy this object. But the management of object life is how important for C programmers. Perhaps Singleton only belongs to a creation mode, the masters believe that there should be "destruction mode" here.

Some people think Sinleton should be destroyed when the program is exited. But when should I exit? Please see the following code: Suppose it is to write an object SINGLTON object in the way in the design mode.

Class Singlton {private: static singlton * _insatnce; sINGLTON () {cout << "Object IS [" << this << "] do "<< Endl; _insatnce = 0;

STATIC SINGLTON * getsinglton () {if (_insatnce) return_insatnce; _insatnce = new singlelton; return_insatnce;} void dosomething () {cout << "Object is [" << "]" <<】 do something "<< }};

SINGLTON * SINGLTON :: _ inSatNCE = NULL;

Void foo (INT i) {/ *

Program body * / if (i) singlton :: getsingLTON () -> dosomething (); / *

Program body * /} int main () {/ * void; program body * / foo (1)

// if (SINGLTON :: _ inSatnce) // singlton :: getsingLTON (); You can't compile delete Singlton :: getsingLTON ();}

In fact, if Foo (1) is called at all in SINGLTON, it is only called Foo (0), but it must also be invoked when the last program exits actually call getsingLton () to build an object. deleted. This is completely unnecessary. Want to use the judgment statement when the program is executed. Such implementations can also be improved, using a static member function CheckexistInstance in Singlton to determine if the object exists and can improve efficiency. However, in this way, the interface has increased the interface and increases the overhead of the code maintenance.

But the object is not clear when the program is over, it is no longer needed. We will modify the code as follows.

Class Singlton {private: static singlton * _insatnce; sINGLTON () {cout << "Object IS [" << this << "] do << Endl; _insatnce = 0;} static saleslton * getsinglton () {if (_insatnce) return_insatnce; _insatnce = new salesLton; return_insatnce;} void dosomething () {COUT << "Object is [" << this << "] do something" << endl;}};

SINGLTON * SINGLTON :: _ inSatNCE = NULL;

Void foo (INT i) {/ *

Program body * / if (i) singlton :: getsingLTON () -> dosomething (); / *

Program body * /}

Class testsingleton {public: tests :: getsinglton () - -> dosomething ();} ~ testsingleton () -> dosomething () -> DOSMETHING (); // A memory leak}}; testsingleton _TEST; int main () {/ * void; program body * / foo (1);

Delete Singlton :: getsinglton ();

And look at what the object applied for TestSingleton should be released. Thus, some people advocate the use of references to be implemented by imitation COM's release. Implementation code Class Singleton {private: static int m_ref; static singleton * _instance; public: void dosomething () {cout << "Object IS [ "<< this <<"] do something "<< Endl;

Static Singleton * getsinglton () {if (_instance) { m_ref; return_instance;} _instance = new singleton; m_ref; return _instance;} ulong release () {--M_REF; if (0 == m_ref; = Delete _INSTANCE; _INSTANCE = NULL; RETURN 0;} return m_ref;} private: Singleton () {cout << "Object IS [" << this << "] do << Endl;}}; singleton * singleton :: _ instance = null; int singleton :: m_ref = 0; void foo () {singleton * P = singleton :: getsinglton (); p-> dosomething (); p-> release ();

INT _TMAIN (int Argc, tchar * argv [], tchar * envp []) {singleton * p = singleton :: getsingLTON (); p-> dosomething (); p-> release ();

Foo (); returnograph 0;

In this way, there is no memory leak, and this code surface is a single piece; in fact, the Singleton object is established and destroyed multiple times. If this object is not like the above code, if it is in single Apply for unmisting memory in the pieces, however, how much the above code is! More If you record the status variable like the number of usage, that's worse. In fact, the realization of single-piece is not as difficult than imagining. We also look

Class Singlton {private: SINGLTON () {cout << "Object is [" << this << "] do << endl;}

Static Singlton & GetSinglton () {static singlton s; return s;} void dosomething () {cout << "Object IS [" << this << "] do something" << endl;}}; void foo ) {/ *

Program body * / if (i) singlton :: getsingLTON (). Dosomething (); / *

Program body * /} Class testsinglton {public: tests :: getsinglton (). Dosomething () (). Dosomething ();} ~ testsinglton () {SINGLTON :: getsinglton (). Dosomething (); int main () {/ * Void; program body * / testsinglt2; foo (1); return 0;}

One of the techniques used here is to use static variables, which is clearly the following benefits:

1) If the single piece is not used at this time, the object is not created. 2) Requires the user to care about the object of the object 3) Fully conform to the design requirements ... I think it is a lot of people dream of. :)

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

New Post(0)