c – 为什么我们可以在null shared_ptr上调用reset?
发布时间:2020-12-16 10:04:26 所属栏目:百科 来源:网络整理
导读:C引子展示了一个例子: auto nos = result[word]; if (!nos) nos.reset(new std::setint); 其中result是std :: map string,shared_ptr std :: set int. 我的问题是:if!nos hold,是否为null?那为什么我们可以在这个null shared_ptr上调用reset方法? 解决
C引子展示了一个例子:
auto &nos = result[word]; if (!nos) nos.reset(new std::set<int>); 其中result是std :: map< string,shared_ptr< std :: set< int>>>. 我的问题是:if!nos hold,是否为null?那为什么我们可以在这个null shared_ptr上调用reset方法? 解决方法
是的,它是null,它没有指向任何数据意义,但它仍然是一个有效的shared_ptr对象.在示例代码中,您使用有效指针重新设置它.
或者,您可以这样写: auto &nos = result[word]; if (!nos) nos = std::make_shared<std::set<int>>(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |