In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to achieve the singleton model on C++. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Singleton mode: there is only one instance.
Singleton pattern singleton mode: ensures that a class can generate only one instance while the program is running, and provides a global access point to it. This class is called a singleton class. For example, in a project, where there is only one database access object, only one mouse can be connected to the computer, and only one window manager can be connected to the operating system, you can consider using singleton mode.
As we all know, in C++, when a class object is created, the compiler allocates memory space for the object and automatically calls the constructor, which initializes the members, that is to say, the constructor is used to initialize the object.
1. Then we need to set the constructor to a private private, which prevents others from using the constructor to create other instances.
2. If the singleton class wants to provide this instance to the system all the time, then it needs to be declared as a static instance member, and the instance will be created only when needed.
3. The static member should be set to null, and it can be judged in a public method that only when the static instance member is null, that is, it is not initialized, it is initialized only once.
Usually we can have a global variable make an object accessible, but it doesn't prevent you from instantiating multiple objects. If you use global or static variables, it will affect the encapsulation, so it is difficult to guarantee that other code will not affect the global variables.
One of the best ways to do this is to make the class itself responsible for saving its only instance. This class ensures that no other instance can be created, and it can provide a method to access the instance, the singleton pattern is better than the global object, and the singleton class can inherit.
The singleton model is divided into two basic situations: hungry Chinese and lazy Chinese.
Initialize the instance directly in the static area, and then return it through the get method, so that the class directly becomes an object at a time, like a hungry man who hasn't eaten for a long time, eager to eat, an urgent new object, which is called a hungry singleton class. Or new instance in the get method, and then return this object, like lazy Chinese characters, do not take the initiative to do things, need to call the get method, only new the object, this is called lazy singleton class.
The following is a sample class Singleton {public: static Singleton * getInstance () {if (instance = = NULL) {instance = new Singleton ();} return instance;} private: / / private constructor to prevent outsiders from calling Singleton () {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.