简体   繁体   English

类/结构设计; 继承与前向声明

[英]class/struct design; inheritance vs forward declaration

I'm having trouble understanding when/if single/multiple inheritance should be used vs forward declaration & I'd like to get a better understanding of it in the following example. 我无法理解何时/是否应该使用单/多继承vs前向声明我想在下面的示例中更好地理解它。 If someone could point me in the right direction, or explain differences between them, it would be much appreciatd. 如果有人可以指出我正确的方向,或解释他们之间的差异,那将是非常值得赞赏的。

Following is sample of data structure I'm dealing with; 以下是我正在处理的数据结构示例;

There exists 1 Parent, 1 SubParent & many different Children types. 存在1个Parent,1个SubParent和许多不同的 Children类型。 ie there's many Children per SubParent per Parent. 即每个父母每个子父母有很多子女。 Some Children need access to other Children objects. 有些孩子需要访问其他儿童对象。 Children will use similar functions to each other ie ParseSelf/Display/Save/Edit. 孩子们将使用相似的功能,即ParseSelf / Display / Save / Edit。 If Children get changed (ie Edit/Save), also need to update data in SubParent, Parent & possibly other Children. 如果儿童被更改(即编辑/保存),还需要更新SubParent,Parent和可能的其他子项中的数据。

I'm wanting to know what would be best way to implement something like this? 我想知道实现这样的最佳方法是什么? I've come up with two alternatives below. 我在下面提出了两种选择。 If more clarification/information is needed please let me know. 如果需要更多说明/信息,请告诉我。

class SubParent;
class Children;

class Parent
{
    ...
    int iParent;
    SubParent *pSubParent;
};

class SubParent
{
    ...
    int iSubParent;
    std::list<Children> childrenList;
};

class Children
{
    ...
    int iChild;
    int ChildType;
    char *pData;
};

versus; 与;

class Parent
{
    ...
    int iParent;
};

class SubParent : public Parent
{
    ...
    int iSubParent;
};

class Children : public SubParent
{
    ...
    int iChild;
    int ChildType;
    char *pData;
};

Thank you. 谢谢。

EDIT: Main job of Parent; 编辑:父母的主要工作; maintains information about file ie location of SubParent, SubParent size, total size of all data including self. 维护有关文件的信息,即SubParent的位置,SubParent大小,包括self在内的所有数据的总大小。 Main job of SubParent; SubParent的主要工作; contains information about Children ie size & location of Children. 包含有关儿童的信息,即儿童的大小和位置。 Both Parent/SubParent don't really do much else except keep these items upto date. 除了保持这些项目最新之外,Parent / SubParent都没有做太多其他事情。

I would recommend going with the first approach, with the following changes: 我建议采用第一种方法,并进行以下更改:

  1. Add a member in SubParent that points to the Parent . SubParent中添加指向Parent的成员。 This will allow you traverse the objects both ways. 这将允许您以两种方式遍历对象。

     class SubParent { ... int iSubParent; Parent* parent; std::list<Children> childrenList; }; 
  2. You should keep a list of Children* in SubParent instead of a list of Chilren if you are going to have different types of Children . 如果你想要有不同类型的Children你应该在SubParent保留一个Children*列表,而不是Chilren列表。 If Children will never have any sub-classes, having a list of Children is good enough. 如果Children永远不会有任何子课程,那么拥有一份Children名单就足够了。

  3. Add a pointer to the SubParent in Children . Children添加指向SubParent的指针。 Again, that will allow you to traverse the objects both ways. 同样,这将允许您以两种方式遍历对象。 Given a Children , you would be able to access its siblings through that pointer. 给定一个Children ,你将能够通过该指针访问其兄弟姐妹。

     class Children { ... int iChild; int ChildType; SubParent* subParent; char *pData; }; 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM