So if I understand correctly, from SOLID design principles, every class should keep a single responsibility.
So there should be one class that creates and manages a resources, a second class that does resource processing/update, the third class that connects the resource to some other resource and so on.
The question is, how do you cleanly express that in code?
Because, in C++, the class destruction order plays an important role.
So you basically come to a situation where you get
ResourceClassInstanceManager cf;//does the ResourceClass creation, accounting and destruction? ResourceClass &rcInstance = cf.GetResourceClass();//creates the instance of resource ResourceClassUpdater rcu(&rcInstance);//updater that works on the class instance ResourceConnector rcc(&rcInstance, &instanceOfAnotherClass);
The problem here is that if cf leaves the scope first, or rcInstance is deleted before rcu or rcc. The other classes will still reference the nonexisting instance of ResourceClass.
This seems somewhat messy, as the whole creation/destruction chain has to be followed very carefully, and becomes really troubling as soon as you have pointers to objects and deeper nesting levels.
Is there a clean solution for this design problem?