0

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
niil87
  • 47
  • 5
  • If you were asked: how would you write a function so that the 2nd time it gets called it throws an exception, how would you write that? It seems a fairly simple, straightforward, programming task, right? So, what do you believe is different between that, and the singleton factory function? – Sam Varshavchik Jul 25 '23 at 22:09
  • "disabling the assignment operator and copy constructor, by having both the methods under private." - Better/more explicit would be to mark them as deleted - `= delete;` since C++11. – Jesper Juhl Jul 25 '23 at 22:11
  • Btw; IMHO singletons should be avoided. They are nothing more than glorified global variables, with all the same problems. – Jesper Juhl Jul 25 '23 at 22:12
  • No, this is not possible. And the singleton flavor suggested by your link isn't threadsafe and uses an unnecessary heap allocation, prefer [Meyer's singleton](https://stackoverflow.com/q/1661529/2752075). – HolyBlackCat Jul 25 '23 at 22:15
  • Instead of returning a raw pointer to the instance, rather return a proxy object to the instance, and make that proxy object not copy-able or assign-able. – Eljay Jul 25 '23 at 22:27
  • @SamVarshavchik, I think I just figured it out. Static variable flag under private, and toggle flag when the default constructor is called. Next time the constructor is called, I can assert. – niil87 Jul 25 '23 at 22:53
  • What do you perceive as the *benefit* of disallowing multiple pointers to the same instance? I can't see any. – user207421 Jul 26 '23 at 00:40
  • @user207421, to be honest, I was just thinking of how tight I can make these checks. Not much experience on what potential problems could arise. – niil87 Jul 26 '23 at 23:44

1 Answers1

2

I am familiar with a singleton class concept

Ok, so these answers to your questions will not dwell further into the depth of singletons.

It is possible to prevent creating multiple copies of the pointer that points to the same object?

No, that's not within your control - unless you control the whole chain where the pointer is revealed.

Assuming we use the default constructor method of creating objects, can I delete the default constructor after creating the first object?

No, that's not at all possible.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108