I've been wondering about this for a while now and everything I read doesn't seem to explain what 'common practice' is in these situations, so any opinions/advice would be helpful (parashift says to use common sense, but I'm a DIYer so that's why I'm here).
The question is about what to do with classes that have complicated member objects which refer to each other to a certain degree. I'm talking about something like this:
Because of the amount of data held in class User and class Dependent, it's very preferential to be able to construct them using initialisation lists. That construction can very easily be done if the noted dependencies are observed. In other words, sending an instance of class Session (this) to each of the members' constructors is ideal.
Session(); // ideally, all member objects are initialised in an initialisation list
Output out; // all following members depend on this being initialised
User user; // requires dbh to be initialised
Dependent depended; // requires this to be initialised
From what I've read, it's possible to do that as long as the member constructors only use members that have already been initialised in the initialisation list. Apparently that's not good style, and I'd rather not do it in case my memory glitches in the future.
The only (reasonable) solution I can think of is to have the members as pointers and to allocate them via new and then delete them later. There's an added complication with doing that though (sigh). The session object is supposed to be persistent (retaining, say the db connection and the output object), but renew, for example, the `user` and `depended` objects. Deleting them when renewing could be done, but what about `~Session()` then? That has to make sure it doesn't delete them too... Is there a good way of being safe with this? Do I need bools in the class noting if the member objects are set or not? Is that not inefficient?
I imagine this is a common problem, so any 'general wisdom' would be much appreciated.