Thread: Complicated member objects that depend on `this`

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Complicated member objects that depend on `this`

    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:
    Code:
    class Session
    {
    public:
        Session(); // ideally, all member objects are initialised in an initialisation list
    
        Output    out;  // all following members depend on this being initialised
        DBClass   dbh;
        User      user; // requires dbh to be initialised
        Dependent depended; // requires this to be initialised
    };
    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.

    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'd rather not do it in case my memory glitches in the future.
    You are referring to you remembering not to change the order, not computer memory, right? Computer memory has nothing to do with it, it will always work if you declare them in the proper order. I don't see a problem with it as long as you make a clear comment to that effect. Hopefully your class doesn't have too many data members that would make it confusing. If it does, consider breaking down that class (the four members in your posted Session class seem fine).

    >> Deleting them when renewing could be done, but what about `~Session()` then? That has to make sure it doesn't delete them too...
    If you make pointers null after you delete them, then you can safely delete them again without error. Of course, I would imagine that a renew function would delete the original memory, but then allocate more in its place, so the destructor would be fine anyway.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Thanks for the reply. I was talking about my own memory, yes. My perceptive processor didn't pick that ambiguity up, sorry. (How did it get to this??)

    So even though it's not good style to use `this` before the constructor's finished, it's still ok? So the initialisation list is guaranteed to be done in the order it's coded and only to pass on to the next item of the list if the previous items have been correctly constructed?

    Would you have a particular preference - between having member objects passed `this` in the parent object's initialisation list and allocating pointer member objects via new - or would you consider that situation-dependent?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, I misread your problem. Sorry about that. I thought you were sending 'out' to 'user' and 'depended'. I didn't have a problem with it because 'out' was already completely constructed by the time 'user' and 'depended' are constructed.

    If you are going to use the Session object in the constructor of 'out', 'dbh', 'user' or 'depended', even if you are only using members that have already been constructed, I would advise against the use of this.

    Instead, just pass the objects that need to be used. If 'user' depends on 'out', pass 'out' instead of 'this'.

    The only time I would pass 'this' to a constructor in the initialization list is if the pointer was being saved only and not used until later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template question
    By grscot in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2003, 03:12 PM
  2. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  3. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM
  4. Modifying CDocument Member Objects :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 04-12-2002, 05:52 PM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM