Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice for a child object to have a pointer to its parent?

Tags:

c++

oop

In a C++ application, let's say I have a window class, which has several instances of a control class. If my window wanted to notify a control that it had been clicked, I might use:

control[n]->onClick();

Now let's say that the control needs to know the size of it's parent window, or some other information. For this I was considering giving the control a pointer to itself (this) as a parameter to it's constructor. I would then make a call like this from the controls onClick() method:

Size windowsize = parent->getSize();

Would this be considered bad practice, or in any other way contradict the values of object orientated programming? If so, what would he the 'proper' way of doing this?

As a side question, would it be better to have a vector of Class or Class*? Is it worth the added complexity for the speed gain? (Changes to the vector would be infrequent).

like image 249
Alexander Rafferty Avatar asked Sep 16 '10 06:09

Alexander Rafferty


People also ask

Which relationship present when child object gets killed if parent object is killed?

What is it called where child object gets killed if parent object is killed? Explanation: Composition occurs when child object gets killed if parent object gets killed. Aggregation is also known as strong Aggregation.

What are parent and child objects?

When discussing objects a "parent-child" relationship implies a hierarchy of objects which can be represented as a tree with parents possessing strong references to their children. If you can draw that tree of objects a "parent" would be closer to the root while a "child" would be closer to a leaf node.


3 Answers

No, it's completely fine. The only issue is that it increases the level of coupling between instances. Also, if you consider usage of smart pointers as advised above, be sure that you make the reference to parent 'weak'. Assuming your window trees are not too deep, you could consider to determine the parent dyuamica1ly, starting from a known top window.

like image 58
Paul Michalik Avatar answered Sep 22 '22 20:09

Paul Michalik


You can consider a hierarchy of controls as being a tree-like graph data structure; when you visualize it that way, it's quite reasonable for a control to have a pointer to its parent.

As for whether objects or pointers to objects should be stored in a vector, well, it depends. You should usually prefer to store objects, but there are a lot of times that you can't do so or it's impractical to do so. For example, if you need to take advantage of polymorphism and store different types of things all derived from a common base class, you'll need to use pointers.

If you do store pointers, make sure to either use a smart pointer of some kind or a pointer container; otherwise, exception safety is a beating.

like image 12
James McNellis Avatar answered Oct 22 '22 19:10

James McNellis


That's fine. It's a common pattern in UI frameworks. E.g., the .NET Windows Forms Control class has a constructor for specifying the parent (http://msdn.microsoft.com/en-us/library/wawy06xc.aspx).

like image 8
Mark Cidade Avatar answered Oct 22 '22 18:10

Mark Cidade