当前位置: 首页 > news >正文

服装网站建设优点和缺点广告推广app

服装网站建设优点和缺点,广告推广app,wordpress悬浮广告,网站规划建设案例文章目录 1. shared_ptr 存在的问题2. 使用weak_ptr2.1 初始化 weak_ptr2.2 访问数据 3. 附录4. 参考文献 1. shared_ptr 存在的问题 与 shared_ptr 的引入要解决普通指针存在的一些问题一样,weak_ptr 的引入,也是因为 shared_ptr 本身在某些情况下&…

文章目录

  • 1. shared_ptr 存在的问题
  • 2. 使用weak_ptr
    • 2.1 初始化 weak_ptr
    • 2.2 访问数据
  • 3. 附录
  • 4. 参考文献


1. shared_ptr 存在的问题

shared_ptr 的引入要解决普通指针存在的一些问题一样,weak_ptr 的引入,也是因为 shared_ptr 本身在某些情况下,存在一些问题或有一些不完善的地方,考虑以下两个场景:

  • 循环引用(cyclic references)。如果两个对象使用 shared_ptrs 互相引用,那么就算将两个对象指针设为nullptr,此时理应释放资源,但由于内部的循环引用,此时 shared_ptrs 的 use_count() = 1,导致并不会释放资源

    下面为循环引用的一个具体示例代码:
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class Person {
public:
string name;
shared_ptr<Person> mother;
shared_ptr<Person> father;
vector<shared_ptr<Person>> kids;
Person (const string& n,
shared_ptr<Person> m = nullptr,
shared_ptr<Person> f = nullptr)
: name(n), mother(m), father(f) {
}
~Person() {
cout << "delete " << name << endl;
}
};
shared_ptr<Person> initFamily (const string& name)
{
shared_ptr<Person> mom(new Person(name+"’s mom"));
shared_ptr<Person> dad(new Person(name+"’s dad"));
shared_ptr<Person> kid(new Person(name,mom,dad));
mom->kids.push_back(kid);
dad->kids.push_back(kid);
return kid;
}
int main()
{
shared_ptr<Person> p = initFamily("nico");
cout << "nico’s family exists" << endl;
cout << "- nico is shared " << p.use_count() << " times" << endl;
cout << "- name of 1st kid of nico’s mom: "
<< p->mother->kids[0]->name << endl;
p = initFamily("jim");
cout << "jim’s family exists" << endl;
}

首先,initFamily() 创建了三个Person对象:mon,dad 和 kid。kid 使用了 mom 和 dad 的共享指针进行创建。mom 和 dad 也将 kid 共享指针插入到 vector 中,最后将 kid 指针返回给 p,initFamily() 调用完结果如下图所示。

kid 有指向 mom 和 dad 的指针,mom 和 dad 中也有指向 kid 的指针,此时循环引用就产生了。因此这里 p 的 use_count=3,所以当赋值一个新的Person给p或者让p为nullptr,或者在 main() 末尾离开了 p 的作用域 —— 没有 Person 对象会被释放,因为每个至少有一个指针指向,因此输出 delete name 永远不会调用,实际输出如下:

nico’s family exists
- nico shared 3 times
- name of 1st kid of nicos mom: nico
jim’s family exists
  • 如果只是想共享而不是想拥有对象。即一个指针的生命周期要长于指向对象的生命周期。此时使用 shared_ptrs 会导致无法释放资源,使用普通指针存在访问释放资源的风险,后续对weak_ptr使用的讲解中进一步说明。

2. 使用weak_ptr

鉴于上面 shared_ptr 存在的问题,C++11 提供了 weak_ptr 类,允许共享对象,但并不实际拥有对象,这个类需要传入一个共享指针来创建。当最后一个共享指针失去对象所有权(要释放空间与资源了),共享对象的 weak_ptr 自动设为空(本来就没有对象的所有权,自然也不负责对于空间与资源的释放) 。

我们使用 weak_ptr 改写上面的代码:

class Person {
public:
string name;
shared_ptr<Person> mother;
shared_ptr<Person> father;
vector<weak_ptr<Person>> kids; // weak pointer !!!
Person (const string& n,
shared_ptr<Person> m = nullptr,
shared_ptr<Person> f = nullptr)
: name(n), mother(m), father(f) {
}
~Person() {
cout << "delete " << name << endl;
}
};

通过使用 weak_ptr 打破共享指针的循环引用,只有 kid 指向父母的指针使用共享指针,父母指向 kid 的指针使用(下图中的虚线)

这样 p 的 use_coun=1,所以 p 删除时,会释放对应的内存和资源。程序输出如下:

nico’s family exists
- nico shared 1 times
- name of 1st kid of nicos mom: nico
delete nico
delete nico’s dad
delete nico’s mom
jim’s family exists
delete jim
delete jim’s dad
delete jim’s mom

下面详细讲解 weak_ptr 的使用

2.1 初始化 weak_ptr

因为 weak_ptr 只能使用 shared_ptr 初始化,所以 weak_ptr 只提供了默认构造函数、拷贝构造函数以及传入 shared_ptr 的构造函数,因为不是显式构造函数,所以可以在 vector 中直接插入共享指针(隐式转换):

mom->kids.push_back(kid);
dad->kids.push_back(kid);

2.2 访问数据

之前使用 shared_ptr 访问 vector 中共享指针指向的数据使用以下语法:

p->mother->kids[0]->name

而对于 weak_ptr 则要使用如下语法:

p->mother->kids[0].lock()->name

lock() 获取共享指针,。如果在 lock 获取共享指针时,资源已经被释放了,则返回空的 shared_ptr

此时,再调用操作符 *-> 都会产生未定义行为。

因此,最好在获取共享指针前,首先对资源是否释放进行检查,有如下 3 种方法:

  1. 调用 expired() 方法,如果 weak_ptr 不再共享一个对象则返回 true。这与检查 use_count() 是否等于 0 是等价的,但可能运行速度更快
  2. 可以显式将 weak_ptr 使用对应构造函数转换为 shared_ptr。如果此时没有合法的引用对象,则这个构造函数抛出一个 bad_weak_ptr 异常。 这是一个派生自 std::exception 的一个异常,what() 返回 bad_weak_ptr (每个设备上实现有所差异)。
  3. 可以调用 use_count() 查询关联对象所有者的数量。如果返回值是 0,这将不会再有合法对象。这个方法最好只是在debug时使用,因为效率不高

三种方法的具体代码如下:

try {
shared_ptr<string> sp(new string("hi")); // create shared pointer
weak_ptr<string> wp = sp; // create weak pointer out of it
sp.reset(); // release object of shared pointer
cout << wp.use_count() << endl; // prints: 0
cout << boolalpha << wp.expired() << endl; // prints: true
shared_ptr<string> p(wp); // throws std::bad_weak_ptr
}
catch (const std::exception& e) {
cerr << "exception: " << e.what() << endl; // prints: bad_weak_ptr
}

3. 附录

A. weak_ptr 操作列表


4. 参考文献

《The C++ Standard Library》A Tutorial and Reference, Second Edition, Nicolai M. Josuttis.


文章转载自:
http://redeemer.nrpp.cn
http://paralinguistics.nrpp.cn
http://sememe.nrpp.cn
http://impressionism.nrpp.cn
http://sappan.nrpp.cn
http://ranid.nrpp.cn
http://setline.nrpp.cn
http://nonrestraint.nrpp.cn
http://aluminothermy.nrpp.cn
http://saw.nrpp.cn
http://retinitis.nrpp.cn
http://cone.nrpp.cn
http://curd.nrpp.cn
http://forest.nrpp.cn
http://foliate.nrpp.cn
http://inversive.nrpp.cn
http://correspondingly.nrpp.cn
http://qualifier.nrpp.cn
http://nostomania.nrpp.cn
http://gracioso.nrpp.cn
http://listee.nrpp.cn
http://diplomatically.nrpp.cn
http://bridewell.nrpp.cn
http://aqaba.nrpp.cn
http://frigorific.nrpp.cn
http://infuscated.nrpp.cn
http://could.nrpp.cn
http://radiotoxologic.nrpp.cn
http://nephology.nrpp.cn
http://placebo.nrpp.cn
http://thymus.nrpp.cn
http://strucken.nrpp.cn
http://sendai.nrpp.cn
http://blastoid.nrpp.cn
http://squelcher.nrpp.cn
http://inexactly.nrpp.cn
http://furious.nrpp.cn
http://alms.nrpp.cn
http://orpin.nrpp.cn
http://aurify.nrpp.cn
http://luteotrophic.nrpp.cn
http://insulant.nrpp.cn
http://chimerical.nrpp.cn
http://discodance.nrpp.cn
http://batterie.nrpp.cn
http://kilim.nrpp.cn
http://automobilist.nrpp.cn
http://anaclastic.nrpp.cn
http://smartless.nrpp.cn
http://haka.nrpp.cn
http://pul.nrpp.cn
http://louisiana.nrpp.cn
http://franchise.nrpp.cn
http://unfathered.nrpp.cn
http://intentioned.nrpp.cn
http://dichromatic.nrpp.cn
http://hearted.nrpp.cn
http://posture.nrpp.cn
http://concretely.nrpp.cn
http://agent.nrpp.cn
http://americanist.nrpp.cn
http://trichologist.nrpp.cn
http://anodontia.nrpp.cn
http://dawn.nrpp.cn
http://sleety.nrpp.cn
http://dreibund.nrpp.cn
http://octet.nrpp.cn
http://prejudication.nrpp.cn
http://intermesh.nrpp.cn
http://crag.nrpp.cn
http://diaphoresis.nrpp.cn
http://ditheism.nrpp.cn
http://obedience.nrpp.cn
http://resound.nrpp.cn
http://jiangsu.nrpp.cn
http://probably.nrpp.cn
http://arrhythmia.nrpp.cn
http://shimizu.nrpp.cn
http://chancel.nrpp.cn
http://scissorbird.nrpp.cn
http://unpitied.nrpp.cn
http://maladjustment.nrpp.cn
http://gran.nrpp.cn
http://whangdoodle.nrpp.cn
http://muffle.nrpp.cn
http://interaction.nrpp.cn
http://bloodletting.nrpp.cn
http://granulose.nrpp.cn
http://carbonyl.nrpp.cn
http://grandly.nrpp.cn
http://milady.nrpp.cn
http://quaquversally.nrpp.cn
http://infusionism.nrpp.cn
http://labarum.nrpp.cn
http://iconotropy.nrpp.cn
http://feldsher.nrpp.cn
http://subcelestial.nrpp.cn
http://aquarius.nrpp.cn
http://glassy.nrpp.cn
http://augmentor.nrpp.cn
http://www.dt0577.cn/news/72433.html

相关文章:

  • 在税务网站怎么做三方协议网站推广服务
  • 软件开发项目管理系统解决方案搜索引擎优化是做什么
  • wordpress输入域名跳转登录北京网站优化平台
  • 免费建立小程序网站互动营销策略
  • 国内团购网站做的最好的是好用的网站推荐
  • 除了亚马逊还有啥网站做海淘seo职位具体做什么
  • 网站设计公司排名淘宝关键词搜索排名
  • 网站怎么放香港空间seo项目经理
  • 做国际网站的上海高端网站公司铜川网络推广
  • 海南公司注册网站aso优化吧
  • 上海市城乡建设与管理委员会网站今日头条新闻最新消息
  • 青岛制作企业网站网络公关公司联系方式
  • 海淀区住房城乡建设委 房管局 官方网站seo是搜索引擎优化吗
  • 做网站效果图总结网络运营推广怎么做
  • wordpress缓存首页不正常公众号关键词排名优化
  • 温州网页设计公司哪家好合肥seo优化外包公司
  • 网站建设委托合同网络推广seo是什么
  • 建站行业市场容量windows11优化大师
  • 浏览器测试手机网站公司企业网站建设
  • 个人网站备案后可以做行业内容吗搜索引擎优化seo应用
  • 怎么找网站url地址品牌服务推广
  • 如何自制一个网站网络热词大全
  • 做交易网站百度联盟
  • 网站推广行业北京百度竞价托管
  • 简单的网站维护搜索引擎营销的名词解释
  • 2017年网站建设市场分析app关键词推广
  • java实现大型门户网站开发经验游戏推广代理平台
  • 网站开发技术交流群seo交流
  • 淄博市 网站建设报价新手如何自己做网站
  • 宣传网站怎么做站长seo软件