C + + Singleton mode

Source: Internet
Author: User

Flyfish 2014-12-16

Write a singleton class

Class Singleton () {}; This class is an empty class

After the compiler processes, the C + + compiler writes the following function code by default

Class Singleton ()     {Public:singleton () {}                                 //1 constructor Singleton (const singleton& s{}                 //2 copy constructor ~singleton () {}                                 //3 destructor singleton& operator= (const singleton& s{}      //4 copy Assignment function copy assignment};

1 preventing users from building objects

The constructor of the Singleton class, the copy constructor, and the copy assignment function are all declared private so that the user does not have the right to establish the object
Private
Singleton () {};
Singleton (Singleton const&);
void operator= (Singleton const&);


2 users can call

Access using the public member function

interface function static singleton& getinstance ()


3 thread safety, guaranteeing the unique object
Static member object (static member objects) is not part of an object
Starting from c++11 If a static member is declared as thread_local, each thread has an object of this kind, otherwise the static member object in the entire program has only one instance, that is, if the static member is declared in C++11, then the static member is thread-safe.

There are two options for when to create an object
Eager Evaluation and Lazy Evaluation
Eager evaluation is the object to be created when the program starts
The quickest calculation is not to calculate, the need is to start creating objects, do not need to create objects that is lazy evaluation (lazy Evaluation)
The purpose of the Lazy evaluation is to minimize the work that the computer is going to do.

C + + Lazy Evaluation
Implemented as follows

Class Singleton{public:static singleton& getinstance () {static Singleton    Instance;return instance;} Private:singleton () {}; Singleton (Singleton const&); void operator= (Singleton const&);};
compiler does not support c++11, you can refer to the implementation of boost
The location of the code in the Boost folder
Boost\core\noncopyable.hpp
Boost\serialization\singleton.hpp


The simplification can be seen clearly
Class Noncopyable{protected:noncopyable () {}~noncopyable () {}private:noncopyable (const noncopyable&); noncopyable& operator= (const noncopyable&);}; typedef noncopyable_::noncopyable NONCOPYABLE;CLASS Singleton_module:public boost::noncopyable{private:static BOOL & Amp Get_lock () {static BOOL lock = False;return lock;} public:static void Lock () {Get_lock () = true;} static void Unlock () {Get_lock () = false;} static bool Is_locked () {return Get_lock ();}}; Template<class t>class singleton_wrapper:public t{public:static bool M_is_destroyed;~singleton_wrapper () {m_is _destroyed = true;}}; Template<class t>bool detail::singleton_wrapper< T >::m_is_destroyed = false;template <class T>class Singleton:public singleton_module{private:static T & instance;static void use (T const &) {}static T & get_in Stance () {static detail::singleton_wrapper< T > t; Boost_assert (! detail::singleton_wrapper< T >::m_is_destroyed); use (instance); return Static_caSt<t &> (T);} Public:static T & Get_mutable_instance () {Boost_assert (! is_locked ()); return get_instance ();} static const T & Get_const_instance () {return get_instance ();} static bool Is_destroyed () {return detail::singleton_wrapper< T >::m_is_destroyed;}}; Template<class t>t & singleton< t >::instance = singleton< t >::get_instance ();



C + + Singleton mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.