In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 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 how C++ creates a subclass of the Singleton class with detailed content and clear logic. 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.
Create a subclass of the Singleton class
The main problem is not so much defining a subclass as creating the only instance of it so that customers can use it. In fact, variables that point to singleton instances must be initialized with an instance of a subclass. The simplest technique is to decide which piece you want to use in the Instance operation of Singleton. An example in the code examples section shows how to implement this technique with environment variables.
It should be roughly as follows:
There are two points to make:
GetClassType is used in the code to determine which instance of the class to use, as well as other methods, or even macro definitions. Exactly what to do depends on the actual needs.
The above code introduces the header file of the derived class into the Cpp file of the base class. This is a problem, but for the sake of simplicity, I'm afraid it's the only way to do it.
Another way to select a subclass of Singleton is to separate the implementation of Instance from the parent class (that is, MazeFactory) and put it into the subclass. This allows C++ programmers to determine the class of the singleton at the link time (that is, by linking to an object file that contains different implementations), but conceals this from the singleton's customers.
Where do the Singleton classes register themselves? One possibility is in their constructors. For example, the MySingleton subclass can do something like this:
Of course, this constructor will not be called unless the class is instantiated, which reflects the problem that the Singleton schema is trying to solve! In C++ we can define a static instance of MySingleton to avoid this problem. For example, we can define in the file that contains the MySingleton implementation:
Static MySingleton theSingleton
The Singleton class is no longer responsible for creating singletons. Its main responsibility is to make the selected singleton object accessible in the system. Static object methods still have a potential drawback-that is, all possible instances of Singl eton subclasses must be created, otherwise they will not be registered.
That's all of the article "how C++ creates a subclass of the Singleton class". 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.