两个派生类之间的聚合
Can there be an aggregation relationship type between two derived classes of one base class (for example, one class contains vector of another)?
Can it be implemented in C++ and if so, is it considered a good practice and does not violate logic or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是肯定的,您的班级图显示了您何时这样做的示例。除了按钮是实际的“物理”项目,并且具有唯一的身份,您不能仅在数据结构中复制它们,您可能会使用这些按钮使用指示,在C ++中使用这些指针。智能指针:
使用指针还允许
myWindow
包含其他myWindow
,而无需您遇到有关不完整类型的编译器错误。Short answer is yes, and your class diagram shows an example of when you'd do this. Except buttons are actual, "physical" items on screen, and have unique identity, you can't just copy them in a data structure, you would probably use pointers to these buttons, in C++ like this using smart pointer:
Using pointers also allows for example
MyWindow
contain otherMyWindow
without you getting compiler errors about incomplete type.