简体   繁体   English

提升智能指针设计问题

[英]Boost Smart pointers design issue

This is my first time experiencing smart pointers in c++ and i have some understanding issues. 这是我第一次体验c ++中的智能指针,我有一些理解问题。 I want to design some kind of a Component Based OpenGL framework. 我想设计某种基于组件的OpenGL框架。

So I have a Scene class which creates Nodes ( and stores the for itself ) and return it as a shared_ptr. 所以我有一个Scene类创建节点(并存储自己)并将其作为shared_ptr返回。 Node class has a list of Components which are created in Node class and again returned as a shared_ptr BUT these Components also need a reference to their owner (Node) and the Node itself should pass its pointer to its Components. Node类有一个在Node类中创建的组件列表,并再次作为shared_ptr返回,但这些组件还需要对其所有者(Node)的引用,Node本身应该将其指针传递给它的Components。

Now: the pointer to the Node in its components, what should it be? 现在:指向其组件中Node的指针,应该是什么? a shared_ptr or a weak_ptr. 一个shared_ptr或一个weak_ptr。 and how to pass that wthitin the Node itself ( Node has no reference to its shared_ptr which is stored in the Scene ) 以及如何在节点本身传递该节点(节点没有引用存储在场景中的shared_ptr)

//! //! EDIT 1 编辑1

class Scene
{
vector<shared_ptr<Node>>  nodes;
public:
weak_ptr<Node>  NewNode();
}

class Node
{
vector<shared_ptr<Component>> components;
public:
weak_ptr<Component> AddComponent();
weak_ptr<Component> GetComponent(String classname);
vecotr<weak_ptr<Component>> GetComponents(String classname);
}

class Component
{
weak_ptr<Node> owner;
Component(weak_ptr<Node> owner_refernce);
public:
// component stuff
weak_ptr<Node> GetOwner();
friend class Node;
}

Please help me to make the best design here. 请帮我在这里做出最好的设计。

// EDIT1 : to be more clear! // EDIT1:更清楚!

Scene is the only OWNER of a Node. 场景是节点的唯一所有者。 It creates and destroys them. 它创造并摧毁它们。 Each Node OWNS all of its Components. 每个节点都拥有其所有组件。 It creates and destroys them. 它创造并摧毁它们。 And the Components need a reference to their Owners which they will share with other objects ( by returning the owner in Get method ). 组件需要引用其所有者,他们将与其他对象共享(通过在Get方法中返回所有者)。

-> Scene owns Nodes and Nodes own Component. - > Scene拥有节点和节点自己的Component。

-> Scene shares Nodes ( return Node object by name ) - >场景共享节点(按名称返回Node对象)

-> Node shared all its Components ( return Component by classname ) - > Node共享其所有组件(按类名返回Component)

-> Components share their owner ( return owner reference ) - >组件共享其所有者(返回所有者参考)

How can i share Objects without sharing the ownership? 如何在不共享所有权的情况下共享对象? weak_ptr ?? weak_ptr ??

EDIT 2: It seems smart pointers are no use in this case. 编辑2:在这种情况下似乎没有用智能指针。 so I am going with row pointers for now 所以我现在要使用行指针

From your description, it sounds like Node instances have a clear owner ( Scene ). 根据您的描述,它听起来像Node实例具有明确的所有者( Scene )。 If so, shared_ptr isn't necessarily the best solution; 如果是这样, shared_ptr不一定是最好的解决方案; std::unique_ptr , or even raw pointers (with the necessary deletes in ~Scene ) would probably be more appropriate. std::unique_ptr ,甚至是原始指针(在~Scene有必要的删除)可能更合适。 Or perhaps Boost's pointer vector. 或者也许是Boost的指针向量。 The same considerations apply to Component and Node . 相同的注意事项适用于ComponentNode And the parent pointers in Component look like they're strictly for navigation, so raw pointers would be the most appropriate. 并且Component的父指针看起来像是严格用于导航,因此原始指针将是最合适的。

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

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