[C ++ Basics] Singleton Mode

Source: Internet
Author: User

1. The purpose of the singleton mode is to ensure that a class has only one instance. 2. Principle (1) Private Static pointer variable implementation: Use the Private Static pointer variable of the class to point to the unique instance of the class and obtain the instance using a public static method. (2) Static local variable implementation: defines a static local variable pointing to the class in the public static method and returns the static local variable. 3. Implement 3.1.1. feature A. It has the only private static member pointer m_pInstance pointing to the class. B. It has a public static getInstance Method for exposing the Singleton. C. The constructor is private to avoid creating such instances from other places. D. Define the private built-in class CGarbo In the singleton class and release the singleton pointer in its destructor. E. Define an instance of the CGarbo class as a static member variable. The program end system will automatically analyze all global variables to automatically release the singleton pointer. 3.1.2. example [cpp] // Singleton1.h # pragma once class CSingleton1 {public: static CSingleton1 * getInstance () {if (m_pInstance = NULL) m_pInstance = new CSingleton1 (); return m_pInstance ;} private: CSingleton1 (); static CSingleton1 * m_pInstance; class CGarbo // The only function is to delete m_pInstance {public :~ CGarbo () {if (CSingleton1: m_pInstance! = NULL) {delete CSingleton1: m_pInstance ;}}; static CGarbo m_garbo; // after the program ends, the system automatically calls its destructor }; [cpp] // Singleton1.cpp # include "StdAfx. h "# include" Singleton1.h "CSingleton1 * CSingleton1: m_pInstance = NULL; // definition of static member variables CSingleton1: CGarbo CSingleton1: m_garbo; // CSingleton1: CSingleton1 () {} 3.1.3. the memory leakage detection uses vld to detect memory leaks. If the CGarbo class is not added, the memory leaks one byte, that is, the one byte occupied by the empty Singleton class instance, as shown below. After the CGarbo class and static member variables are added, no memory leakage occurs. [Html] view plaincopyVisual Leak Detector Version 2.3 installed. WARNING: Visual Leak Detector detected memory leaks! ---------- Block 1 at 0x003AC038: 1 bytes ---------- Call Stack: d: \ microsoft visual studio 9.0 \ projects \ testcpp \ testsingleton \ singleton1.h (11): TestSingleton.exe! CSingleton1: getInstance + 0x7 bytes d: \ microsoft visual studio 9.0 \ projects \ testcpp \ testsingleton. cpp (12): TestSingleton.exe! Wmain f: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ crtexe. c (583): TestSingleton.exe! _ TmainCRTStartup + 0x19 bytes f: \ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ crtexe. c (403): TestSingleton.exe! WmainCRTStartup 0x7C81776F (File and line number not available): kernel32.dll! RegisterWaitForInputIdle + 0x49 bytes Data: CD 3. 2. Static local variable implementation 3.2.1. features A. You do not need to consider the memory release issue. B. Disable class copy and class assignment. 3.2.2. example [cpp] // Singleton2.h # pragma once class CSingleton2 {public: static CSingleton2 & getInstance () {static CSingleton2 instance; return instance;} private: CSingleton2 (); CSingleton2 (const CSingleton2 &); CSingleton2 & operator = (const CSingleton2 &) ;}; 3.2.3. no memory leakage problems

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.