草庐IT

weak_ptr_cast

全部标签

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++ 删除 static_cast<void*> (指针)行为

假设代码执行以下操作:T*pointer=newT();deletestatic_cast(pointer);结果是什么?未定义,内存泄漏,内存被删除? 最佳答案 行为未定义。关于delete表达式,C++标准说:Inthefirstalternative(deleteobject),ifthestatictypeoftheoperandisdifferentfromitsdynamictype,thestatictypeshallbeabaseclassoftheoperand’sdynamictypeandthestaticty

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++ STL:将派生虚拟类用作 std::sort() 的 "Strict Weak Ordering"

我使用std::sort()撞墙了。我有一个纯虚类(名为Compare),方法的调用者派生自该类(名为MyComp)。我将纯虚拟类用于我的API原型(prototype):voidObject::DoSort(Compare&comp){std::sort(this->mKeys.begin(),this->mKeys.end(),comp);}来电者:classMyComp:publicCompare{booloperator()(constRow*r1,constRow*r2){...}}cmp;...obj->DoSort(cmp);Linux上的g++编译器提示:“无法分配类型

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++ - 关于static_cast的问题

我写了一段代码,但我对它的输出感到困惑:#includeusingnamespacestd;classB{public:virtualvoidfoo(){cout(pb);pd1->foo();pd1->disp();}intmain(intargc,char*argv[]){B*pb=newB();func(pb);return0;}输出是:B::fooD::disp但是据我所知,pb指向类型B。而且里面没有名为disp()的函数?那么,为什么它可以访问D类中的disp()函数? 最佳答案 因为disp()不访问类的任何成员,原则

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_