Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How C++ creates a subclass of the Singleton class

Shulou Source: shulou.com Published: 2022-06-01 03:36:00 10月04日 Update

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.

Tags: Subclasses instances singletons files knowledge articles questions code objects methods caches differences contents variables customers are yes static centering constructors Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Microsoft Apple MariaDB