在 C++ 中将智能指针放入类数据(作为类成员)中的正确方法是什么?

发布于 2024-09-14 07:33:10 字数 392 浏览 1 评论 0原文

假设我有一个类 Boda:

class Boda {
    ...
};

并且我在这个类中有一个成员 cydo,我希望它成为一个智能指针(也就是说,我希望它在类被销毁时自动释放) 。

我正在使用Boost的智能指针,所以我写道:

class Boda {
    boost::shared_ptr<int> cydo;
    public:
        Boda () {
            cydo = boost::shared_ptr<int>(new int(5));
        }
};

这是将智能指针作为类成员的正确用法吗?

谢谢,博达·西多。

Suppose I have a class Boda:

class Boda {
    ...
};

And I have a member cydo in this class that I want to be a smart pointer (that is, I want it to get deallocated automatically as soon as the class gets destroyed).

I am using Boost's smart pointers, so I write:

class Boda {
    boost::shared_ptr<int> cydo;
    public:
        Boda () {
            cydo = boost::shared_ptr<int>(new int(5));
        }
};

Is this the correct use of putting smart pointers as class members?

Thanks, Boda Cydo.

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

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

发布评论

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

评论(2

偏爱你一生 2024-09-21 07:33:10
class Boda {
    boost::shared_ptr<int> cydo;
    public:
        Boda () : cydo(new int(5)) {} 
};

不过,我不明白为什么你想包装一个 int ...:)

class Boda {
    boost::shared_ptr<int> cydo;
    public:
        Boda () : cydo(new int(5)) {} 
};

Though, I can't think why you'd want to wrap an int ... :)

夏尔 2024-09-21 07:33:10

您应该问自己的真正问题是是否需要将成员与类分开分配。与存储任何类型的指针相比,仅将成员存储在类中通常会更好。如果你做不到这一点,例如,如果成员的生命周期在创建容器之前或之后开始,可以扩展到成员的所有权之外,可以屈服于其他对象,那么你必须使用指针。

一旦需要使用指针,您应该更喜欢智能指针而不是原始指针,并根据您的要求选择特定类型。如果成员所有权不与其他对象共享,但需要使用指针,因为所包含对象的生命周期可能在容器的生命周期之前或之后开始,或者如果所有权可以转移给其他对象,但管理资源(除非产生)是容器的唯一责任,那么首选 unique_ptrauto_ptr

如果包含的对象不仅仅属于此类,则使用shared_ptr。如果该成员由不同的线程使用,也可以使用此方法,每个线程都持有自己的 shared_ptr,即使所有权仅保留在一个线程中,以避免在一个线程中销毁该对象它仍在另一个中使用。

The real question you should ask yourself is whether you need to allocate the member separately from the class. It is usually better if you just store the member in the class --as compared to storing any type of pointer. If you cannot do it, if for example the lifetime of the member starts before or after the container is created, can be extended beyond of ownership of the member can be yield to other objects, then you must use pointers.

Once you need to use pointers, you should prefer smart pointers to raw pointers, and select the particular type based on your requirements. If the member ownership is not to be shared with other objects, but you need to use a pointer because the lifetime of the contained object may start before or after that of the container, or if ownership can be transferred to other objects, but management of the resource (unless yielded) is the sole responsibility of the container, then prefer unique_ptr or auto_ptr.

If the contained object does not belong solely to this class then use a shared_ptr. This can also be used if the member is used by different threads, with each thread holding its own shared_ptr even if the ownership is held in only one of the threads, to avoid destroying the object in one thread when it is still being used in another.

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