草庐IT

my_shared_ptr

全部标签

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++ - CreateFile 失败并出现错误 ERROR_SHARING_VIOLATION

我使用CreateFileapi,有时它随机失败并出现错误:ERROR_SHARING_VIOLATION。我用谷歌搜索了一下,几乎没有关于这个错误的信息。奇怪的是下次打开同一个文件还是蛮开心的。这是我的代码:voidFileHandle::open(constchar*fileName,FILE_MODEmode){if(m_bIsOpen)close();HANDLEfh=NULL;DWORDdwDesiredAccess=GENERIC_READ;DWORDdwShareMode=FILE_SHARE_READ;DWORDdwCreationDisposition=OPEN_EX

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++ : Read a file name from the command line and utilize it in my file

如何从命令行读取文件名并在我的C++代码文件中使用它?例如:./cppfileinputFilenameoutputFilename非常感谢任何帮助! 最佳答案 intmain(intargc,char**argv){stringinFile="";stringoutFile="";if(argc==3){inFile=argv[1];outFile=argv[2];}else{cout 关于C++:Readafilenamefromthecommandlineandutilizeiti

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>