简体   繁体   English

Class 类型为 `std::shared_ptr 的变量成员图<t> `</t>

[英]Class diagram for variable member with the type of `std::shared_ptr<T>`

class Foo{};

class Demo
{
public:
    std::shared_ptr<Foo> foo_ptr{new Foo()};;
};

What's the relationship between Foo and Demo? Foo和Demo有什么关系? I think it is an aggregation.我认为这是一个聚合。 Am I right?我对吗? How to draw such the relationship between them?怎么画出他们之间这样的关系呢?

On thing is sure: the shared_ptr implements an association.可以肯定的是: shared_ptr实现了一个关联。 The association is moreover navigable from Demo to Foo .该关联还可以从Demo导航到Foo

Be we can't tell more with certainty:我们不能确定地说更多:

  • The shared_ptr could implement a shared aggregation (hollow diamond). shared_ptr可以实现共享聚合(空心菱形)。 But we can neither confirm nor infirm, because the UML specs do not define the semantics of shared aggregation (I know, it's difficult to believe given its heavy use).但我们既不能证实也不能证实,因为 UML 规范没有定义共享聚合的语义(我知道,鉴于它的大量使用,很难相信)。 In practice, there is no real benefit to use shared aggregation over a simple association in a model.实际上,与 model 中的简单关联相比,使用共享聚合并没有真正的好处。
  • The shared_ptr could in theory even be used to implement composite aggregation. shared_ptr理论上甚至可以用于实现复合聚合。 It's not very likely, even less with a public pointer.这不太可能,使用公共指针时更不可能。 But, in theory, the code could ensure that only a single shared_ptr is active for a given component.但是,理论上,代码可以确保只有一个 shared_ptr 对于给定组件处于活动状态。 This would not be immediately visible and would require some code analysis, but cannot be excluded at least if the shared_ptr would be private.这不会立即可见,需要进行一些代码分析,但至少如果 shared_ptr 是私有的,则不能排除。

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

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