✎ 编程开发网
首页C语言C++面试Linux函数Windows数据库下载搜索
当前位置:首页 -> 基础 -> c++编程基础

智能指针 (五)

2014-11-23 23:11:57 · 作者: · 浏览: 19
标签: 智能 指针
weak_ptr waptr(aptr);
aptr->bptr = bptr;
bptr->aptr = waptr;
cout << "ref count of bptr: " << bptr.use_count() << endl;
cout << "ref count of aptr: " << waptr.use_count() << endl;
cout << "end" << endl;
}
int main()
{
test();
cout << "after test" << endl;
} 运行结果:
[plain]
begin
ref count of bptr: 2
ref count of aptr: 1
end
ref count of B: 1
deconstruct A
ref count of A: 0
deconstruct B
after test
begin
ref count of bptr: 2
ref count of aptr: 1
end
ref count of B: 1
deconstruct A
ref count of A: 0
deconstruct B
after test 可以看到A和B都被析构了,并且在test函数的结尾处,检查aptr的引用计数是1,而bptr是2,这是因为weak_ptr指向这个对象,不会引起引用计数的改变。
首页 上一页 2 3 4 5 下一页 尾页 5/5/5
上一篇 C/C++基础知识:typedef用法小结
下一篇 C/C++中宏总结C程序的源代码中可..
Copyright © https://www.cppentry.com all rights reserved 粤ICP备13067022号-3