Probe into a single piece of C ++

zhaozj2021-02-16  37

Recently, I have read the masterpiece of Adrianx "C realizes a single piece of single piece", it is really good, but there is a hurt.

The design analysis of its Singleton class is as follows:

First, let us test the Singleton class:

#include using namespace std;

// In order to give the SINGLETON class, Template Class Counter {public: counter () { count;} ~ counter ()};

Counter (const counter &) { count;} static size_t hoWMANY () {Return Count;}

PRIVATE: Static size_t count;

Class Singleton: Private Counter {private: singleton () {cout << "Object IS [" << this << "] <

~ Singleton () {cout << "Object is [" << this << "] DO DESTRUCTION> << endl;

Static singleleton & getsingleton () {static singleleton s; return s;} void dosomething () {cout << "Object is [" << "<<"] do something "<< endl;}};

INT Main (int Argc, char * argv []) {singleton & first = Singleton :: getsingleton ();

Singleton Second (first); // 1

Singleton third (first); // 2

Cout << "there" << singleton :: howmany () << "."

Return 0;}

Results of the:

Object is [0049f1c0] DO Object is [0012FEBF] do destruction> Object is [0049f1c0] do destruction>

It can be seen that Singleton has generated three instance, but construction is only called once, it is not difficult to see that the other two instance is constructed by the Copy Ctor, here we have found this small in this program. Bug, but how to remove this 'bug'? When we don't define your COPY CTOR, the compiler will generate a default Copy Ctor when needed. Analyze the above code, the usage of both Singleton Pattern, 1, 2 should be prohibited, and it is easy for 1, 2, but it should be prohibited). But once it is interested or unintentionally used in the program, functions are used.

It is not easy to be discovered by us. This situation we should try to expose them in Compile-Time. We can definitely define this by way of the DESTRUCTOR to Private Section. Because Singleton's Destructor belongs to Private Section, Compiler will complain! then...

But for some classes, Destructor is not required, and DEStructor will reduce Performance (although that is just a little bit, but since we can avoid, why is it not?). So by changing the DESTRUCTOR, Access Level is not very wise! However, how should we prevent behavior such as 1, 2? Let me think ...

Ok! We have to avoid the generation of the compiler to synthesize the generation of Default Copy Constructor, how can we achieve this? Fortunately, it is not difficult to avoid this behind-the-scenes operation of the compiler.

And see the modified Singleton class as follows:

Class Singleton: Private Counter {//

Private: // preventions Singleton (Const Singleton &); Singleton & Operator = (Const Singleton);

I declare the Copy CTOR and Assignment Operator of Singleton as Private and do not provide definition (this is to avoid being called by Friend or MEMBER FUNCTIONS). This way we can make it clear that 1, 2 is out of COMPILER! The Singleton class is a relatively perfect implementation of Singleton Pattern.

The full code is as follows:

Class singleton {private: singleton () {cout << "Object IS [" << this << "] do << endl;}

Static Singleton & GetSingleton () {static singleleton s; return s;} void dosomething () {cout << "Object IS [" << this << "] do something" << endl;} private: // preventions singleleton Const Singleton &); Singleton & Operator = (Const Singleton);

At this point, our Singleton class is designed! There is still an omissions of it, but also hope that you don't care!

Wanjunfeng@gmail.com

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

New Post(0)