Hello everyone:) Recently I've got a task for which I need some suggestions on best way to implement it. The task is as follows: I need to add a container, existing in single instance only, which will be available in some to other classes in system. The constraint is that however I'm able to modify classes' implementation, the creation of class instances is held inside a framework which is provided as a compiled library only and thus can't be amended and class instances are not available either. For this reason by-the-book Dependency Injection pattern is not applicable here. An alternative approach would be to utilize Singleton pattern, which I personally not a fan of. I'm not sure what is best way do this. Would you guys please share some ideas with me? Thanks))
So you're suggesting a singleton implementation, pointers rather than references, new without any promise of delete and ifs which don't use braces? All of these things are commonly advised against.
@shadowmouse I use pointers there is nothing wrong with that . if with no braces means only one instruction , that is why I add a newline right after. But I will edit the destructor I forgot to add :)
If you are going to use GoF singleton, register cleanup function (which destroys object) to be called on exit by atexit() handler.
I use pointers there is nothing wrong with that
Using owning raw pointer is a sign of extremely bad style in modern C++. RAII was created in 80s.
Also you forgot copy-move constructor/assigments. And exposed destruction function for everyone to use with no way to determine who is in responsible of destroying object
@ MiiNaPaa thanks for your suggestion)) unfortunately I have no access to class instances so wrappers won't make it (maybe I misunderstood you). regarding singleton: I would prefer not to use it at, however if this is my best bet, I could change my mind.
I though only creation is handled by libraries and you can manipulate instances later. If this isn't hte case, then yes, wrappers cannot be used here.
So your best bet is either a singleton or non-creatable class (interface) one instance of each is accessible through function (which gets own copy from some factory) (JLBorges approach).
Another alternative is a PIMPL with shared implementation. Kind of singleton, but you can easily add ability to use several instances without changing interface at all.