简体   繁体   English

子对象具有指向其父对象的指针是不好的做法吗?

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

In a C++ application, let's say I have a window class, which has several instances of a control class. 在C ++应用程序中,假设我有一个窗口类,其中有一个控件类的多个实例。 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. 为此,我正在考虑为控件提供一个指向自身(this)的指针,作为其构造函数的参数。 I would then make a call like this from the controls onClick() method: 然后,我将从控件的onClick()方法进行如下调用:

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*? 作为附带问题,拥有Class或Class *的向量会更好吗? Is it worth the added complexity for the speed gain? 值得为速度增加更多的复杂性吗? (Changes to the vector would be infrequent). (对向量的更改很少)。

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. 否则,异常安全将是一个打击。

That's fine. 没关系。 It's a common pattern in UI frameworks. 这是UI框架中的常见模式。 Eg, the .NET Windows Forms Control class has a constructor for specifying the parent ( http://msdn.microsoft.com/en-us/library/wawy06xc.aspx ). 例如,.NET Windows窗体Control类具有用于指定父级的构造函数( http://msdn.microsoft.com/zh-cn/library/wawy06xc.aspx )。

In fact the GOF Composite design pattern is based on Explicit Parent References. 实际上, GOF Composite设计模式基于显式父级引用。

Explicit parent references. 显式的父引用。 Maintaining references from child components to their parent can simplify the traversal and management of a composite structure. 维护子组件到父组件的引用可以简化复合结构的遍历和管理。 The parent reference simplifies moving up the structure and deleting a component. 上级参考简化了上移结构和删除组件的过程。 Parent references also help support the Chain of Responsibility (223) pattern. 父级引用也有助于支持责任链(223)模式。 The usual place to define the parent reference is in the Component class. 定义父引用的通常位置是在Component类中。 Leaf and Composite classes can inherit the reference and the operations that manage it. Leaf和Composite类可以继承引用及其管理操作。

With parent references, it's essential to maintain the invariant that all children of a composite have as their parent the composite that in turn has them as children. 使用父代引用,必须保持复合材料的所有子代都具有作为其父代的复合材料的不变性,而复合材料又将它们作为子代。 The easiest way to ensure this is to change a component's parent only when it's being added or removed from a composite. 确保这一点的最简单方法是仅在将组件添加到组合中或从组合中删除时才更改其父级。 If this can be implemented once in the Add and Remove operations of the Composite class, then it can be inherited by all the subclasses, and the invariant will be maintained automatically. 如果可以在Composite类的Add和Remove操作中一次实现此方法,那么它可以被所有子类继承,并且不变量将自动维护。

Therefore, I guess, there is a clear place for such a design, depending on the actual requirement and context. 因此,我认为,根据实际需求和上下文,这样的设计会有明确的位置。

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. 假设您的窗口树不是太深,则可以考虑从已知的顶部窗口开始确定父级。

It is OK. 没关系。 However, make sure you need it because it can complicate your code in situations where a childs parent can change. 但是,请确保您需要它,因为在子代父母可以更改的情况下,它会使您的代码复杂化。 Examine why the child needs to know who its parent is and consider the costs and alternatives. 检查孩子为什么需要知道其父母是谁,并考虑费用和替代方案。 An example alternative for your scenario would be: Resizing can come from the top down when a parents window is resized it can tell its children to resize by iterating and calling a method or setting a property. 您的方案的另一种示例是:当调整父窗口的大小时,调整大小可以自上而下进行,它可以通过迭代和调用方法或设置属性来告诉其子项重新调整大小。 When a child needs to resize (like a textbox that can grow based on the data in it) it can raise an event to anyone listening saying that it resized and the parent could listen for that event. 当孩子需要调整大小时(例如可以根据其中的数据增长的文本框),它可以向任何正在收听的人发起一个事件,要求说调整大小,而父母可以听该事件。

如果需要这样做,请考虑将父级作为const*传递。

It's not bad. 不算太差。 For example, in Qt widgets every widget will have its pointer to parent, even without using const* . 例如,在Qt小部件中,即使不使用const* ,每个小部件都将具有指向父级的指针。

BTW: It is somewhat confusing since you said "parent" people may misunderstood by hierarchy parent. 顺便说一句:这有点令人困惑,因为您说过“父母”可能会被等级制度的父母误解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 让子对象引用其父对象而不是指针是不好的做法吗? - Is it bad practice to have a child object have a reference to its parent object instead of a pointer? 具有指向其父级的指针的对象是否应定义了复制构造函数? - Should an object with a pointer to its parent have a copy constructor defined? 在指向父对象的指针上初始化子对象 - Initialize child object on a pointer to parent 重新初始化指针是不好的做法吗? - Is it bad practice to reinitialize a pointer? 有一个没有自己的 get 方法的 set 方法是不好的做法吗? - Is it bad practice to have a set method without its own get method? 在实践中,从父母到孩子的 dynamic_cast 什么时候有用? 这总是不好的做法吗? - When is dynamic_cast from parent to child useful in practice? Is this always a bad practice? 子对象调用父对象的良好实践方式 - Good practice way of child object calling parent object 将子对象指针更改为父指针(无内存泄漏) - Changing child object pointer to parent pointer (without memory leak) 是否有任何方法可以在Child范围内指向父类(派生的类)? - Is there any way to have a pointer to a parent (derived) class inside the scope of the Child? 拥有一个带有原始指针访问器的unique_ptr成员是不好的做法吗? - Is it bad practice to have a unique_ptr member that has a raw pointer accessor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM