I would like to figure out pro and contra of different ways to deal with the issue of owning and not-owning raw pointers mixed up, within the pre-C++11 OOP framework that I am using. My role "as framework user" is basically to implement a dozen of abstract classes among the huge class hierarchy provided by the framework. This framework (and the "user code examples" from which I start):
- extensively create objects in the heap via factories/singletons or explicit
new
, and most of these objects are managed by the framework but others are users' responsibility - and you have to inspect the examples user code and cross-check the thick user manual to be sure which - examples suggests to embed other classes in your implementation classes always via pointer (which you create in constructors in the proper way and, if you are the owner, delete in destructor)
- all interrelation between classes is via pointer, I mean that most of the methods takes non-owning raw pointers as parameter to the objects that they have to use.
In MY user code I want to achieve the following GOALS in order of priority:
- Clearly mark which member pointer is an owning pointer, so that I do not have to go through the manual every time that I read again a piece a code
- Have a safer code
- Have a simpler/more readable/more maintainable code
I consider the following options for all member objects of my implementation classes:
A) all owning raw pointers (clearly detectable by the delete
in the destructor but also always cross-checked with the user guide) replaced with unique_ptr.
PRO I achieve all (1), (2) and (3), as e.g. I kick out the trivial destructor, etc
CONTRA I have to add .get()
at every call of the framework methods (plus sometimes some .reset()
when initlization cannot happen in-class/in-initializer-list), which at least at the first glance looks weird, but maybe one has just to get used to.
B) the "softer" approach of the "Guideline Support Library" owner<T*>
"tag".
PRO at least I achieve (1), without diverging too much from the "framework guidelines".
CONTRA I give up with (2) and (3). Further developments will inherit further on this legacy
C) why should I have pointers at all, and not just embed the object inside??? I should get the exact PRO and CONTRA as option (A) - just &
to add instead of .get()
-, right???
Of course the approach departs more substantially from what the framework suggests.
This is what I have found so far, any further overlooked details (especially warnings) or suggestions are really welcome.