Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Suppose that I have

  • three classes P, C1, C2,
  • composition (strong aggregation) relations between P <>- C1 and P <>- C2, i.e. every instance of P contains an instance of C1 and an instance of C2, which are destroyed when the parent P instance is destroyed.
  • an association relation between instances of C1 and C2 (not necessarily between children of the same P).

To implement this, in C++ I normally

  • define three classes P, C1, C2,
  • define two member variables of P of type boost::shared_ptr<C1>, boost::shared_ptr<C2>, and initialize them with newly created objects in P's constructor,
  • implement the relation between C1 and C2 using a boost::weak_ptr<C2> member variable in C1 and a boost::weak_ptr<C1> member variable in C2 that can be set later via appropriate methods, when the relation is established.

Now, I also would like to have a link from each C1 and C2 object to its P parent object. What is a good way to implement this?

My current idea is to use a simple constant raw pointer (P * const) that is set from the constructor of P (which, in turn, calls the constructors of C1 and C2), i.e. something like:

class C1
{
  public:
    C1(P * const p, ...)
    : paren(p)
    {
    ...
    }

  private:
    P * const parent;
    ...
};

class P
{
  public:
    P(...)
    : childC1(new C1(this, ...))
    ...
    {
        ...
    }

  private:
    boost::shared_ptr<C1> childC1;
    ...
};

Honestly I see no risk in using a private constant raw pointer in this way but I know that raw pointers are often frowned upon in C++ so I was wondering if there is an alternative solution.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Pointers create headaches when you use them to manage dynamically allocated resources. In this case, however, a pointer is a simple "backlink" to the parent, and it cannot be changed during the lifetime of a child object (i.e. there's no re-parenting). This is a good solution that helps you keep things simple.

share|improve this answer
    
I had thought about using a weak pointer from child to parent, but in order to initialize it I need to give the child a shared pointer pointing to the parent. But the parent does not have a shared pointer to itself so I discarded this solution. –  Giorgio Oct 3 '12 at 14:53
    
@Giorgio A weak pointer would probably be an overkill in this situation, because the parent fully owns its children. Since children get deleted along with the parent, there is little reason to give them weak pointers. –  dasblinkenlight Oct 3 '12 at 14:56
    
The fact that a weak pointer would be an overkill is not the only problem. To construct a boost::weak_ptr<P> I need to pass it a boost::shared_ptr<P>: how can I get this boost::shared_ptr<P> within the constructor of P? –  Giorgio Oct 3 '12 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.