如何:具有派生类特定方法的派生类中的派生成员

发布于 2024-12-12 14:10:43 字数 763 浏览 2 评论 0原文

我在 Ubuntu 11.10 和最新版本的 NetBeans 下使用 C++。假设我有 以下代码:

class Node {}
class DerivedNode : public Node {}

class Graph {
    vector<Node*> nodes;
}

class DerivedGraph : public Graph { }

目前,我将 DerivedNodes 存储在 DerivedGraph 类中,例如:

nodes.push_back(new DerivedNode());

当我需要使用仅适用于 DerivedNodes 和 DerivedGraphs 的特定方法时 我被迫首先在我的节点指针上使用dynamic_cast。

我希望能够在 DerivedGraph 中拥有仅适用于 DerivedNodes 的特定方法 并避免需要强制转换指针。如果最后我不介意重新设计我的课程 结果比我的要好。

我确信必须有一个干净而简单的方法来实现我正在尝试做的同样的事情。 也许有专门的模板?对此事的任何想法都会非常重要 赞赏。如果我还没有去过的话,我还将提供所需的任何其他信息 清除。

编辑:我没有两份副本。我想强调它的外观。我对这次演讲表示歉意。我想要获得的是:

class DerivedGraph: public Graph {
    vector<DerivedNode*> nodes;
}    

I am using C++ under Ubuntu 11.10 and the latest version of NetBeans. Let's say I have the
following code:

class Node {}
class DerivedNode : public Node {}

class Graph {
    vector<Node*> nodes;
}

class DerivedGraph : public Graph { }

At the moment I'm storing DerivedNodes in the DerivedGraph class like this for example:

nodes.push_back(new DerivedNode());

When I need to use specific methods that only apply to DerivedNodes and DerivedGraphs
I am forced to use a dynamic_cast on my Node pointers first.

I would like to be able to have specific methods in DerivedGraph which apply only to DerivedNodes
and avoid the need of casting pointers. I do not mind having to redesign my classes if the end
result is better than what I have.

I am sure there must be a clean and simple method to achieve the same thing I'm trying to do.
Maybe something with specialized templates? Any thoughts on the matter would be greatly
appreciated. I'll also provide any additional information required in the case I haven't been too
clear.

EDIT: I don't have two copies. I wanted to put emphasis on how it looks. I apologize for the presentation. What I want to obtain is:

class DerivedGraph: public Graph {
    vector<DerivedNode*> nodes;
}    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2024-12-19 14:10:43

确定您在Node中的接口是否合适?有时,当您发现自己需要向下转型时(特别是在基指针存储在容器中的情况下),这可能表明您的抽象接口无法正确满足您的所有需求。通常,像模板方法模式这样的东西可以解决您的所有需求,而根本不需要沮丧。

但是,假设您的继承模型确实需要以这种方式工作,您可能想要做的是在 DerivedGraph 中重写虚拟方法来添加和获取节点。在这种情况下,您必须验证节点类型并将其向下转换。

最后一种方法是拥有两个单独的容器,一个位于父容器中,其中包含所有非 DerivedNode 的节点,然后是 DerivedGraph 中的另一个容器,其中包含所有 派生节点。然后,您再次使用重写的函数来根据您的 API 需求确定要访问哪个容器。

Are you sure that your interface in Node is appropriate? Sometimes when you find yourself needing to downcast (especially in a case like this where base pointers are stored in a container) that may be a signal that your abstract interface doesn't cover all your needs properly. Often something like the Template Method pattern solves all your needs without needing a downcast at all.

However, assuming that your inheritance model really need work in such a way, what you probably want to do is have virtual methods that get overridden in DerivedGraph for adding and getting nodes. You will have to verify the node type and downcast it in this case.

One final approach is to have two separate containers, one in the parent that contains all nodes that aren't DerivedNode and then another container in DerivedGraph that contains all the DerivedNode. Then you use overridden functions again to determine which container to access depending on your API needs.

随波逐流 2024-12-19 14:10:43

首先不要在派生类中复制数据成员。

然后添加用于将数据添加到容器的虚拟成员函数。这样您就可以在派生类中创建派生类型的实例并将它们添加到容器中。

最后,当您重写返回派生类中数据引用的虚函数时,请使用 covariant返回类型

Start by not duplicating your data member in the derived class.

Then add virtual member functions that you use to add data to your container. That way you can create instances of derived types in the derived class and add them to the container.

Finally, when you override the virtual function that returns a reference to data in the derived class, use covariant return types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文