C++11智能指针之shared_ptr
2018-01-26 17:11
330 查看
shared_ptr类似 Objective-C中的【引用概念】,这里分别测试了【引用增加】、【引用撤销】、【唯一性检查】,这里测试动作名称是自己随意叫的,现贴出测试代码及输出结果:
测试代码:
输出结果如下图所示:
其它可参考链接:http://blog.csdn.net/xiejingfa/article/details/50750037
测试代码:
#include <windows.h> #include <iostream> #include <memory> // need include this header file int main(void) { std::shared_ptr<int> pInt(new int(5)); std::cout << "---------------------" << std::endl; std::cout << *pInt << std::endl; std::cout << "pInt count :\t" << pInt.use_count() << std::endl; // check unique if (pInt.unique()) { std::cout << "pInt [is] unique pointer to object " << std::endl; } else { std::cout << "pInt [is not] unique pointer to object " << std::endl; } std::shared_ptr<int> pInt2(pInt); std::cout << "---------------------" << std::endl; std::cout << "pInt count :\t" << pInt.use_count() << std::endl; std::cout << "pInt2 count :\t" << pInt2.use_count() << std::endl; // check unique if (pInt.unique()) { std::cout << "pInt is unique pointer to object " << std::endl; } else { std::cout << "pInt [is not] unique pointer to object " << std::endl; } std::shared_ptr<int> pInt3(pInt); std::cout << "---------------------" << std::endl; std::cout << "pInt count :\t" << pInt.use_count() << std::endl; std::cout << "pInt2 count :\t" << pInt2.use_count() << std::endl; std::cout << "pInt3 count :\t" << pInt3.use_count() << std::endl; pInt2.reset(); std::cout << "---------------------" << std::endl; std::cout << "after pInt2.reset()" << std::endl; std::cout << "pInt count :\t" << pInt.use_count() << std::endl; std::cout << "pInt2 count :\t" << pInt2.use_count() << std::endl; std::cout << "pInt3 count :\t" << pInt3.use_count() << std::endl; pInt2.reset(); std::cout << "---------------------" << std::endl; std::cout << "after pInt2.reset()" << std::endl; std::cout << "pInt count :\t" << pInt.use_count() << std::endl; std::cout << "pInt2 count :\t" << pInt2.use_count() << std::endl; std::cout << "pInt3 count :\t" << pInt3.use_count() << std::endl; pInt3.reset(); std::cout << "---------------------" << std::endl; std::cout << "after pInt3.reset()" << std::endl; std::cout << "pInt count :\t" << pInt.use_count() << std::endl; std::cout << "pInt2 count :\t" << pInt2.use_count() << std::endl; std::cout << "pInt3 count :\t" << pInt3.use_count() << std::endl; // check unique if (pInt.unique()) { std::cout << "pInt is unique pointer to object " << std::endl; } else { std::cout << "pInt [is not] unique pointer to object " << std::endl; } system("pause"); return 0; }
输出结果如下图所示:
其它可参考链接:http://blog.csdn.net/xiejingfa/article/details/50750037
相关文章推荐
- C++11智能指针之shared_ptr的使用(1)
- 【C++11】新特性——共享资源的智能指针shared_ptr
- 【C++11新特性】 C++11智能指针之shared_ptr
- C++11-智能指针-shared_ptr
- C++11 FAQ中文版:共享资源的智能指针——shared_ptr
- C++11 Intro - C++11智能指针之shared_ptr、weak_ptr
- C++11 智能指针——shared_ptr
- C++11 shared_ptr共享智能指针
- (转载)【C++11新特性】 C++11智能指针之shared_ptr
- c++11:智能指针之shared_ptr
- 【C++11新特性】 C++11智能指针之shared_ptr
- c++11之shared_ptr智能指针使用
- 【C++11新特性】 C++11智能指针之shared_ptr
- c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
- C++11 FAQ中文版:共享资源的智能指针——shared_ptr
- 【C++11新特性】 C++11智能指针之shared_ptr
- C++11智能指针之使用shared_ptr实现多态
- C++11智能指针之shared_ptr、weak_ptr
- C++11智能指针之std::shared_ptr