gpt4 book ai didi

c++ - scoped_ptr 所有权

转载 作者:可可西里 更新时间:2023-11-01 16:29:18 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
What is a smart pointer and when should I use one?

我正在阅读 an article我找到了一个小例子来演示 boost::scoped_ptr<T> 的使用:

#include <cstdlib>
#include <iostream>
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>

static int count = 0;

class printer
{
int m_id;

public:
printer(void) :
m_id(count++)
{
}

~printer(void)
{
std::cout << "Printer " << m_id
<< " destroyed" << std::endl;
}
};

int
main(void)
{
boost::scoped_ptr<printer> p1(new printer);
boost::scoped_ptr<printer> p2(new printer);
std::cout << "Exiting test program" << std::endl;

return EXIT_SUCCESS;
}

我在文章中唯一不明白的是这句话:

using scoped_ptr, you indicate that ownership transfer is not intended or allowed.

作为这个主题的初学者,也许这篇文章是错误的,但上面这行到底是什么意思?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com