简体   繁体   English

C++ shared_ptr 操作逻辑

[英]C++ shared_ptr manipulation logic

I have this logic:我有这个逻辑:

struct Foo;
struct Bar;  

struct IComponent {
    virtual Foo * GetFoo() { return nullptr; }
    virtual Bar * GetBar() { return nullptr; }
}

struct Foo : public IComponent {
    Foo * GetFoo() override { return this; }
}

struct Bar : public IComponent {
    Bar * GetBar() override { return this; }
}

The components are managed by组件由

class SimpleObject {
  public:
     void Add(IComponent * b){
        components.push_back(b);
        if (b->GetFoo()) foo = b->GetFoo();
     }

     template <typename T>
     T * GetComponent() const {
        for (size_t i = 0; i < components.size(); i++){
           if (T * tmp = dynamic_cast<T *>(components[i])){
            return tmp;
           }
        }
        return nullptr;
     }
   
  private:
    Foo * foo;
    std::vector<IComponent *> components;
}


template <> Foo * SimpleObject::GetComponent<Foo>() const { 
    return this->foo; 
}

I can have multiple different SimpleObject .我可以有多个不同的SimpleObject However, each of them can contain same or different components (one component can be assigned to multiple SimpleObject ).但是,它们中的每一个都可以包含相同或不同的组件(一个组件可以分配给多个SimpleObject )。 GetComponent is used to only access the associated component. GetComponent仅用于访问关联的组件。 There should be no ownership transfer (however, I dont know how to force this since a library user can, of course incorrectly or my mistake, do this) - components are not aware of each other, only via the SimpleObject they are asociated to.不应该有所有权转移(但是,我不知道如何强制这一点,因为一个图书馆的用户,当然可以不正确或我的错误的,这样做) -组件是不知道对方的,只有通过SimpleObject他们asociated到。

Now, I dont like the raw pointers.现在,我不喜欢原始指针。 I converted std::vector<IComponent*> to std::vector<std::shared_ptr<IComponent>> and void Add(IComponent* b) to void Add(std::shared_ptr<IComponent> b) .我将std::vector<IComponent*>std::vector<std::shared_ptr<IComponent>>并将void Add(IComponent* b)void Add(std::shared_ptr<IComponent> b)

However, I am not sure how to manage GetComponent method.但是,我不确定如何管理GetComponent方法。 Should I also convert its return type to shared_ptr or is it better to stick with the raw pointer and return it via .get() ?我还应该将其返回类型转换为shared_ptr还是坚持使用原始指针并通过.get()返回它更好? The same goes for helper variable foo .辅助变量foo However, in this case, I think that keeping it as a raw pointer is better.但是,在这种情况下,我认为将其保留为原始指针更好。

If you don't want to expose ownership (let alone transfer it) as part of your interface, returning a raw pointer (or some special observer pointer type, if you use such) is plainly the right idea.如果您不想将所有权公开(更不用说转移它)作为接口的一部分,那么返回原始指针(或某些特殊的观察者指针类型,如果您使用此类)显然是正确的想法。 (In particular, avoid constructions like const std::shared_ptr<T>& foo() that meaninglessly express observation in terms of some ownership elsewhere .) This approach also saves a bit of reference count fiddling and is compatible with your explicit specialization. (特别是,避免像const std::shared_ptr<T>& foo()这样的结构在其他地方的某些所有权方面无意义地表达观察。)这种方法还节省了一些引用计数的麻烦,并且与您的显式专业化兼容。

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

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