I am familiar with a singleton class concept, where we use a static pointer to point to one and only one instance of a class.
https://qnaplus.com/singleton-class-in-c/
I am also familiar with disabling the assignment operator and copy constructor, by having both the methods under private.
I am trying to take it further. It is possible to prevent creating multiple copies of the pointer that points to the same object?
Singleton *p = Singleton::Instance();
Singleton *q = Singleton::Instance(); // Can I make this fail?
auto A = p; // Can I make this fail?
Just thinking wildly here if there is no way to restrict the above statements. Assuming we use the default constructor method of creating objects, can I delete the default constructor after creating the first object? The below code is an example that obviously doesn't work.
class Singleton {
public:
Singleton() {
Singleton() = delete;
} // default constructor