In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how C++ realizes Singleton mode. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Here are the implementation issues to consider when using the Singleton pattern:
1) guarantee a unique instance
The Singleton pattern makes this unique instance a generic instance of a class, but the class is written so that only one instance can be created.
The only instance of a class provided by the Singleton pattern is itself a generic instance, but ensures that only one instance is created at the implementation level.
A common way to do this is to hide the operation of creating this instance behind a class operation (that is, a static member function or a class method) that ensures that only one instance is created. This operation accesses the variable that holds the unique instance, and it ensures that the variable is initialized with the unique instance before returning a value. This method ensures that a piece is created and used before it is used for the first time.
The first step to do this is to provide a class operation that creates a unique instance (equivalent to a static member function in C++). This operation manages the variable that holds the unique instance, ensuring that the variable must be initialized by the unique instance before it is returned.
In C++, you can use the static member function Instance of the Singleton class to define this class operation. Singleton also defines a static member variable _ instance, which contains a pointer to its unique instance.
To implement the singleton pattern by using C++, to put it simply, static member functions are used to manage static data members of pointer types. The other point is that here we say "yes" rather than "must".
The class declaration for Singleton is as follows:
The corresponding implementation is
The customer accesses this piece only through the Instance member function. The variable _ instance is initialized to 0, and the static member function Instance returns the value of the variable. If its value is 0, it is initialized with a unique instance. Instance is initialized with lazy; its return value is not created and saved until it is first accessed.
Note that the constructor is protective. Customers who try to instantiate Singleton directly will get a compile-time error message. This ensures that only one instance can be created.
In addition, because _ instance is a pointer to a Singleton object, the Instance member function can assign a pointer to a subclass of Singleton to this variable. We will give such an example in the code examples section.
The above code has been modified in accordance with the new features of Category 11. One is to initialize and compare _ instance using nullptr instead of 0, and the other is to use = delete to disable the generation and use of copy constructors and assignment operators.
There is one more thing to note about the implementation of C++. It is not enough to define a piece as a global or static object and then rely on automatic initialization. There are three reasons:
A) We cannot guarantee that only one instance of a static object will be declared.
B) We may not have enough information to instantiate each piece during static initialization. A single piece may require a value that is calculated later in the execution of the program.
C) C++ does not define the calling order of the constructor of the global object on the conversion unit (translation unit) [E S 9 0]. This means that there is no dependency between individual pieces; if so, errors will be inevitable.
The implementation of using global / static objects has another (albeit minor) drawback, which causes all singletons to be created whether they are used or not. The use of static member functions avoids all these problems.
These are all the contents of this article entitled "how C++ realizes Singleton mode". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.