还在苦苦敲代码开发APP?你out啦! 试试积木搭建APP吧~

auto_ptr is not dereferencable

来源:清泛原创     2016-10-24 17:51:18    人气:     我有话说( 0 人参与)

错误如下图所示:错误代码示例: Example : Transferring ownership from one auto_ptr to anothervoi...

错误如下图所示:

错误代码示例:

// Example : Transferring ownership from
//            one auto_ptr to another
void testAutoPtr6()
{
	std::auto_ptr<TC> pt1(new TC);
	std::auto_ptr<TC> pt2;

	pt1->someFunc(); // OK

	pt2 = pt1;  // now pt2 owns the pointer, and pt1 does not
	std::cout << "Content of pt1 is " << pt1.get() << std::endl;
	std::cout << "Content of pt2 is " << pt2.get() << std::endl;

	pt2->someFunc(); // OK

	pt1->someFunc(); // error! following a null pointer

} // as we go out of scope, pt2's destructor
// deletes the pointer, but pt1's does nothing

auto_ptr在拷贝时会转移内存控制权,例子中pt1赋值给pt2后,将内存管理权转移给pt2, 此时pt1指针为NULL。

auto_ptr dereferencable

注:本文为本站或本站会员原创优质内容,版权属于原作者及清泛网所有,
欢迎转载,转载时须注明版权并添加来源链接,谢谢合作! (编辑:admin)
分享到: