同步操作将从 Huoty/cppstudy 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/** Copyright (c) Huoty, All rights reserved* Author: Huoty <sudohuoty@163.com>* CreateTime: 2020年08月19日 22:23:50**/#include <iostream>#include <memory> // 使用 shared_ptr 需要 include 它using namespace std;int main(void){// 通过 make_shared 创建 shared_ptrstd::shared_ptr<int> p1 = std::make_shared<int>();*p1 = 78;std::cout << "p1 = " << *p1 << std::endl;// 查看引用计数std::cout << "p1 Reference count = " << p1.use_count() << std::endl;// 第二个 shared_ptr 也将在内部指向相同的指针// 这将会使引用计数变为 2std::shared_ptr<int> p2(p1);// 查看引用计数std::cout << "p2 Reference count = " << p2.use_count() << std::endl;std::cout << "p1 Reference count = " << p1.use_count() << std::endl;// 比较智能指针if (p1 == p2) {std::cout << "p1 and p2 are pointing to same pointer\n";}std::cout << "Reset p1" << std::endl;// 重置 shared_ptr,在这种情况下,其内部不会指向内部的任何指针// 因此其引用计数将会变为 0p1.reset();std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;// 重置 shared_ptr,在这种情况下,其内部将会指向一个新的指针// 因此其引用计数将会变为1p1.reset(new int(11));std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;// 分配 nullptr 将取消关联指针并使其指向空值p1 = nullptr;std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;if (!p1) {std::cout << "p1 is NULL" << std::endl;}// 定义时还可以用 auto 简化auto p3 = std::make_shared<int>();cout << "p3 Reference Count = " << p3.use_count() << endl;return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。