草庐IT

pimpl_ptr

全部标签

c++ - 在包含 shared_ptr 的 map 上使用 find_if 会增加引用计数

我正在创建一个程序,它有一个包含shared_ptr的映射。当我尝试使用std::find_if在其中查找元素时,shared_ptr的引用计数会增加。示例:#include#include#include#includeintmain(void){std::map>map;map[1]=std::make_shared(3);map[2]=std::make_shared(5);map[4]=std::make_shared(-2);autoit=std::find_if(map.begin(),map.end(),[](conststd::pair>&elem){std::cout

c++ - 如何在 C++11 中返回包含自定义删除器的 std::unique_ptr?

我的应用程序编译器最多只能支持c++11。下面是我的项目和函数get_conn()的片段代码返回std::unique_ptr和自定义删除器(删除器需要两个参数)。我正在使用auto关键字作为返回类型,但它给出了一个错误,就像ifiscompiledwithc++11(compilesfinewithc++14)error:‘get_conn’functionuses‘auto’typespecifierwithouttrailingreturntype演示示例代码:#include#include#includeusingnamespacestd;//Dummydefinitiono

c++ - auto_ptr 会防止这种情况发生吗?

我不太清楚auto_ptr在这种情况下是否会帮助我:classA{A(constB&member):_member(B){};...constB&_member;};AgenerateA(){auto_ptrsmart(newB());AmyA(*smart);returnmyA;}当smart离开其封闭范围时,myA._member引用是否有效?如果auto_ptr不是这里的答案,那是什么?编辑:我看到我把每个人都弄糊涂了;我必须在范围外返回myA,这就是为什么我关心_member在smart退出范围后是否有效。 最佳答案 这对你

c++ - 在 GCC 中使用 shared_ptr 的可移植方式

GCC4.1使用header和GCC4.3使用header,我需要一种可移植的方式来使用shared_ptr使用GCC4.3.2和GCC4.2.1,有没有办法在不检查GCC版本宏或使用外部库(如Boost)的情况下做到这一点? 最佳答案 仍将与gcc4.3一起工作。如果您想同时支持这两个版本,只需使用tr1姓名。 关于c++-在GCC中使用shared_ptr的可移植方式,我们在StackOverflow上找到一个类似的问题: https://stackove

c++ - weak_ptr 和父子循环依赖

我目前有类似以下内容:classParent{//justasinglechild...forsakeofsimplicity//nootherclassholdsashared_ptrreferencetochildshared_ptr_child;System*getSystem(){...}}classChild{weak_ptr_parent;~Child{_parent.lock()->getSystem()->blah();}}Child析构函数总是崩溃,因为当~Child()运行时_parent总是过期。是否有针对这种怪现象的典型解决方案?简而言之,有没有办法在~Chil

c++ - Pimpl 习语和没有友元声明的内部对象协作

我正在使用pimpl惯用法实现几个类,并且遇到了一些设计问题。首先,我一直看到pimpl是这样做的classObject{public:Visible();~Visible();..etc..private:classObjectImpl*_pimpl;};我有几个使用这种方法的类,我的问题是其中几个类需要访问彼此的实现细节,但_pimpl指针是私有(private)的。谁能看到将_pimpl公开的缺点。显然,如果它是公开的,那么有人可能会不小心(或故意)重新分配它。(我忽略了一个事实,即“私有(private)”可以#defined为“公共(public)”并授予访问权限。如果您这样

c++ - 如何使用 std::auto_ptr 声明动态数组?

我正在尝试声明一个动态int数组,如下所示:intn;int*pInt=newint[n];我可以用std::auto_ptr做到这一点吗?我试过类似的方法:std::auto_ptrpInt(newint[n]);但是它不编译。我想知道我是否可以使用auto_ptr构造声明一个动态数组,以及如何声明。谢谢! 最佳答案 不,你不能,也不会:C++98在数组方面非常有限,auto_ptr是一个非常笨拙的野兽,它通常不会做你需要的事情。您可以:使用std::vector/std::deque,或std::array,或者使用C++11和

c++ - 是否有 auto_ptr 的替代品可以与 c++11 中的 boost ptr_map 一起使用

在c++11中,auto_ptr已弃用,取而代之的是更合理的unique_ptr。唉,如果你使用boost::ptr_map,auto_ptr就完成了一个非常方便的用途:std::auto_ptrpLayer(newLayer());mRawLayerPtrMap.insert(layerName,pLayer);是否有可能使用与c++11类似的东西。这个我知道Layer*pLayer=newLayer();mFusedLayers.insert(fusedLayerName,pLayer);有效,但auto_ptr在一些更复杂的场景中有它的优点。是否有适用于C++11的替代品?

c++ - 为什么 shared_ptr<void> 而不是 shared_ptr<HANDLE>

基于http://en.highscore.de/cpp/boost/smartpointers.html#smartpointers_shared_pointer#include#includeintmain(){boost::shared_ptrh(OpenProcess(PROCESS_SET_INFORMATION,FALSE,GetCurrentProcessId()),CloseHandle);SetPriorityClass(h.get(),HIGH_PRIORITY_CLASS);}问题:为什么h定义为boost::shared_ptr而不是boost::shared_

c++ - std::find 跨一组 shared_ptr

我确定我在这里做了一些愚蠢的事情,但我看不到它。为什么不能编译以下内容?#include#include#include#include//Aclasstoplaywith.Encapsulatesaname.classStringClass{public:StringClass(std::stringconst&name):MyName(name){}std::stringconst&Name()const{returnMyName;}private:std::stringMyName;};//Thesetofinstancesof"StringClass".std::vector>