C ++ object deterministic parsing singleton

The idea is reusable design patterns, we in the programming process, more or less exposure to design patterns, but, sometimes, we have not been met intersection nothing, then we today to explain the singleton pattern, and the attachment of a C ++ programming skills.

We know Singleton pattern in the actual development process is a very useful feature singletons we may know:

1, only one instance of a class

2, to provide a global access point

3, do not copy

Let's analyze one by one:

1, if you want to achieve only one instance, we need to do:

a, the constructor is declared private

2, to provide a global access point

a, create a class static member function

3, do not copy

a, declared private copy constructor, and does not provide implementations

b, the assignment operator declared private

How we tested at the time of writing singleton class it? Two methods:

1, instantiate multiple objects, called several times to see the constructor, if only called once, the instructions to create only one instance

2, single-step tracking, view an object's address, did the same, as compared with a target

I was the dividing line ------------- ------------

In the process of learning C ++, we have long exposed to the constructor and destructor, these two functions are invoked automatically, even if we do not declare the constructor and destructor, the system will generate for us.

Then there is the C ++ programming deterministic destruction, that is to say, no matter how, as long as the object is created, the system will call the destructor, so if we have a class nested in class and outside the class declaration a nested class object, then the object nested class destructor is certain calls, can we do something in the class destructor nested inside it?

Our approach is:

Is defined as a private class Garbo embedded in the singleton Singleton prevent such abuse elsewhere.

The only instance when the run is finished, the system will call the static member Garbo Singleton destructor, the destructor of the singleton ***

Using this method releases singleton object features:

1, the definition of class-specific nested inside singleton;

2, defined specifically for releasing private static class member in a single embodiment;

3, using a program at the end of global variable characteristics destructor, choose the final release timing;

4, an example of using the code does not require any single operation, do not release the object of interest.

code show as below:

I was the dividing line ------------- ------------

include

using namespace std;

When the end of life // static Garbo garbo_ objects, nested class calls the destructor Garbo

// function, as well as the destruction of instance_

// use the principles of object-deterministic destructor

// impose constraints, only one instance, simply constructor declared private

// need a global point of access,

class Singleton

{

public:

static Singleton* GetInstance()

{

if(NULL == instance_)

{

instance_ = new Singleton;

}

return instance_;

}

~Singleton()

{

cout 《 “~Singleton …” 《 endl;

}

/ * This method can also release resources, it is the simplest method

static void Free()

{

if(NULL != instance_ )

{

delete instance_;

}

}

*/

// nested class

class Garbo

{

public:

~Garbo()

{

if(Singleton::instance_ != NULL)

{

delete instance_;

}

}

};
private:

// copy constructor declared as private, you can disable copy, and does not provide an implementation

Singleton(const Singleton& other);

// also prohibits the assignment, the assignment operator declared private

Singleton& operator=(const Singleton& other);

Singleton()

{

cout 《 “Singleton …” 《 endl;

}

// merely stated that if the definition need to be placed outside of class

static Singleton* instance_;

// Create garbo object is declared

static Garbo garbo_;

// use the principles of object-deterministic destructor

};

// Because Garbo is nested class, so the front to add SIngleton

Singleton :: Garbo Singleton :: garbo_; // in front of the static is not required

Singleton* Singleton::instance_;

int main(void)

{

// No matter how many times GetInstance call, return is the same instance

Singleton* s = Singleton::GetInstance();

Singleton* s1 = Singleton::GetInstance();

// Singleton s3 (* s); error copy prohibited

// Singleton s3 = * s; error assignment prohibition

// construct several instances, invoked several constructors, the constructor is called only once

// Singleton :: Free (); release of resources, this is a stupid method

return 0;

}

I was the dividing line ------------- ------------

PS:

1, the above method is not thread safe

2, can also be used to achieve auto_ptr smart pointer, we again late on

3, in addition to nested classes, which also gives the simplest method, we Mining

Published 241 original articles · won praise 3 · Views 3183

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105163495