Single case Mode singleton

Source: Internet
Author: User
Objective: To ensure that there is only one instance object for this class in the program. Method: Use a static object. Important: 1. A class can have only one instance, 2, and it must create the instance itself, 3. It must provide this instance to the entire system on its own.  Implementation method: 1. The singleton mode class only provides a private constructor, 2. The class definition contains a static private object of the class, and 3, which provides a static public function to create or get a static private object of that itself. Application Scenario: Resource Manager C + + code implementation://C10 SingleTon #include <iostream>using namespace std; Class egg{static Egg e; int num; Egg (int x): num (x) {} Egg (const egg&);   Copy constructor public:static egg* getinstance () {return &e;} int val () {return num--;}}; Egg egg::e (+); int main () {//egg x (); Cout<<egg::getinstance ()->val () <<endl; Cout<<egg:: GetInstance ()->val () <<endl; Cout<<egg::getinstance ()->val () <<endl;}

Single case Mode singleton

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.