如何在函数中传递智能指针?

发布于 2025-01-02 22:54:57 字数 508 浏览 3 评论 0原文

在下面的示例中,当我将 p 传递给函数时,一旦函数 func 退出,它就会被销毁。

void func(std::auto_ptr<int> p)
{
   // deletes p
}

int main()
{
    std::auto_ptr<int> p(new int);
    func(p);
    *p = 1; // run-time error
}

我还被告知,通过引用传递智能指针是非常糟糕的设计C++ 标准库 - Nicolai M. Josuttis 参考”。

引用:

允许 auto_ptr 通过引用传递是非常糟糕的设计,你 应该始终避免它.....

.....根据auto_ptrs的概念,可以通过使用常量引用将所有权转移到函数中。

是不可能传递智能指针还是我有错误的想法?

In the following example, when I pass p to a function, It gets destroyed as soon as the function func exits

void func(std::auto_ptr<int> p)
{
   // deletes p
}

int main()
{
    std::auto_ptr<int> p(new int);
    func(p);
    *p = 1; // run-time error
}

I'm also told that passing smart pointers by reference is very bad design from the book "The C++ Standard Library - Reference by Nicolai M. Josuttis".

Quote:

Allowing an auto_ptr to pass by reference is very bad design and you
should always avoid it .....

..... According to the concept of auto_ptrs, it is possible to transfer ownership into a function by using a constant reference.

Is it not possible to pass smart pointers or have I got the wrong idea?

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

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

发布评论

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

评论(2

差↓一点笑了 2025-01-09 22:54:57

是否无法传递智能指针或者我的想法错误?
它仅适用于auto_ptr。此外,根据新的 C++11 标准,auto_ptr 已被弃用,如果您使用的是 c++11,则 unique_ptr 是更好的替代方案。

auto_ptr 模板类确保当控制离开作用域时,它指向的对象会自动销毁,如果您在函数中,一旦函数作用域结束,对象就会被删除。因此,本质上,您将指针的所有权转移给函数,并且在函数调用之外您不拥有该指针。

通过引用传递 auto_ptr 被认为是一种糟糕的设计,因为 auto_ptr 是专门为所有权转移而设计的,通过引用传递它意味着该函数可能会也可能不会接管所有权传递的指针。


对于 unique_ptr,如果您将 unique_ptr 按值传递给函数,那么您将转让该对象的所有权 unique_ptr 到函数。

如果您将 unique_ptr 的引用传递给函数,您只想让函数使用指针,但您不想 将其所有权传递给函数。


shared_ptr 在引用计数机制上运行,因此当调用复制函数时计数总是递增,而当调用析构函数时计数总是递减。

通过引用传递shared_ptr可以避免调用其中任何一个,因此可以通过引用传递它。虽然按值传递它会适当地增加和减少计数,但在大多数情况下,shared_ptr 的复制构造函数并不是很昂贵,但在某些情况下可能很重要,因此使用两者中的任何一个取决于具体情况。

Is it not possible to pass smart pointers or have I got the wrong idea?
It only applies to auto_ptr. Also, as per the new C++11 standard auto_ptr is deprecated and unique_ptr is the superior alternative if you are using c++11.

The auto_ptr template class ensures that the object to which it points gets destroyed automatically when control leaves a scope, If you pass auto_ptr by value in a function, the object is deleted once the scope of the function ends. So essentially you transfer the ownership of the pointer to the function and you don't own the pointer beyond the function call.

Passing an auto_ptr by reference is considered a bad design because auto_ptr was specifically designed for transfer of ownership and passing it by reference means that the function may or may not take over the ownership of the passed pointer.


In case of unique_ptr, If you pass an unique_ptr to function by value then you are transferring the ownership of the unique_ptr to the function.

In case you are passing a reference of unique_ptr to the function if, You just want the function to use the pointer but you do not want to pass it's ownership to the function.


shared_ptr operates on a reference counting mechanism, so the count is always incremented when copying functions are called and decremented when a call is made to destructor.

Passing a shared_ptr by reference avoids calls to either and hence it can be passed by reference. While passing it by value appropriately increments and decrements the count, the copy constructor for shared_ptr is not very expensive for most cases but it might matter in some scenarios, So using either of the two depends on the situation.

烟雨凡馨 2025-01-09 22:54:57

您不能传递 std::auto_ptr,但可以传递一个管理对其引用数量的智能指针,例如 boost::shared_ptr

You cannot pass a std::auto_ptr, but you can pass a smart pointer that manages the number of references to it, like boost::shared_ptr.

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