C + + Singleton mode

Source: Internet
Author: User

#include <iostream> #include <string>using namespace Std;class signalexample{static signalexample* c_ instance;//all constructors (constructors, copy constructs, assignment overloads) are privatized so that external objects cannot be created directly signalexample () {}signalexample (); Signalexample (const signalexample&); signalexample& operator = (const signalexample&);//Create a static privatized member to store a unique singleton object public://static functions for creating unique singleton objects Must be public static so that the static signalexample* getinstance () is called directly before the object is created, void print ();//The detection function for printing object addresses}; signalexample* signalexample::c_instance = NULL; signalexample* signalexample::getinstance () {///If an object has already been created, then return directly, if not, call the constructor if (c_instance = = NULL) {c_instance = new Signalexample ();} return c_instance;} void Signalexample::p rint () {cout<< "address-->this:" &LT;&LT;THIS&LT;&LT;ENDL;}    int main () {signalexample* s = signalexample::getinstance ();    signalexample* S1 = signalexample::getinstance ();    signalexample* s2 = signalexample::getinstance ();    S->print ();    S1->print ();    S2->print (); return 0;}

  

#include <iostream> #include <string> #include "Singleton.h" using namespace Std;class signalexample{Friend Class singleton<signalexample>;//privatize all constructors (constructors, copy constructs, assignment overloads) so that external objects cannot be created directly signalexample () {}signalexample ( Const signalexample&); signalexample& operator = (const signalexample&);//Create a static privatized member to store only one singleton object public:void print ();//    Detection function for printing object addresses};class Test{private:friend class singleton<test>; Test () {} test (const test&) {} test& operator = (const test&);p ublic:void print ();//For Print object address detection function};void test::p rint () {cout<< "address-->this:" &LT;&LT;THIS&LT;&LT;ENDL;} void Signalexample::p rint () {cout<< "address-->this:" &LT;&LT;THIS&LT;&LT;ENDL;}    int main () {signalexample* s = singleton<signalexample>::getinstance ();    signalexample* S1 = singleton<signalexample>::getinstance ();    signalexample* s2 = singleton<signalexample>::getinstance (); test* S4 = Singleton<test>:: getinstance ();    test* s3 = Singleton<test>::getinstance ();    S->print ();    S1->print ();    S2->print ();    S3->print ();    S4->print (); return 0;}

  

#ifndef _singleton_h_#define _singleton_h_template< TypeName T >class singleton{    static t* c_instance;public :    static t* getinstance ();}; template< typename t >t* singleton<t>::c_instance = null;template< typename T >t* singleton<t>:: GetInstance () {    if (c_instance = = NULL)    {        c_instance = new T ();    }    return c_instance;} #endif

  

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.