草庐IT

c++ - 无法为 unique_ptr 返回类型返回 nullptr

我正在为SDL_Texture*原始指针编写一个包装器,它返回一个unique_ptr。usingTexturePtr=std::unique_ptr;TexturePtrloadTexture(SDL_Renderer*renderer,conststd::string&path){ImagePtrsurface=loadImage(path);if(surface){returnTexturePtr(SDL_CreateTextureFromSurface(renderer,surface.get()),SDL_DestroyTexture);}returnnullptr;}但它给

c++ - 如何防止为 unique_ptr 删除 px.get()

在boost.org网站上,我看到了一个阻止deletepx.get()forashared_ptr(http://www.boost.org/doc/libs/1_51_0/libs/smart_ptr/sp_techniques.html#preventing_delete)的示例。这是一项很好的技术,我想在C++11中使用std::unique_ptr来应用它,经过一段时间的工具化后,我无法完全理解他们的示例如何使用std::unique_ptr。是否可以防止在std::unique_ptr上调用deletepx.get()?这是来自boost.org网站的代码,展示了如何防止调

c++ - shared_ptr 的 dtor 是否需要使用 "deleter"?

It'swidelyknown您可以使用shared_ptr来存储指向不完整类型的指针,只要在构造shared_ptr期间可以删除该指针(具有明确定义的行为).例如,PIMPL技术:structinterface{interface();//out-of-linedefinitionrequired~interface()=default;//publicinlinemember,evenifimplicitlydefinedvoidfoo();private:structimpl;//incompletetypestd::shared_ptrpimpl;//pointertoinco

c++ - unique_ptr : linked list entry deletion

我目前正在考虑借助unique_ptr实现单链表。尽管由于析构函数的递归调用(请参阅Stackoverflowwithunique_ptrlinkedlist)可能会出现堆栈溢出的问题,但我还是遇到了以下问题:假设,我们有以下链表的实现structnode{node(void):val(0),next(nullptr){}intval;std::unique_ptrnext;};并且我们已经根据初始化了我们的列表intmain(intargc,char*argv[]){nodeHEAD;HEAD.val=0;autoptr=&HEAD;for(inti=0;ival=i;ptr->ne

c++ - 使用 shared_ptr 时,我应该只使用一次 shared_ptr 声明还是在我传递它的所有地方都声明 shared_ptr?

使用shared_ptr时,我应该只使用shared_ptr吗?申报一次或申报shared_ptr无论我经过哪里?所以在我新建实例的函数中,我将它包装在shared_ptr中但是当我从函数返回它时,我也可以返回一个shared_ptr或者,使用get()在shared_ptr上,只返回一个普通指针。所以我的问题是,我应该只使用shared_ptr吗?当我新建实例然后传递普通指针或者我应该传递shared_ptr时无处不在? 最佳答案 创建一个shared_ptr不会在它的指针对象上赋予魔法力量。神奇之处在于shared_ptr—及其

c++ - unique_ptr 对象的 getter 和 setter(依赖注入(inject))

我有一个MyClass类,它拥有某个DataProvider类的一个实例,并为此提供了一个getter。为了依赖注入(inject),我更愿意有一个getter和一个setter。此外,DataProvider应包含在std::unique_pointer中:#include#includeclassDataProvider{public:DataProvider(){}virtual~DataProvider(){}/*stuff*/private:/*morestuff*/};classMyClass{public:MyClass(){}virtualinlineconstData

c++ - 包含 shared_ptr 的对象的常量正确性

考虑对象:classObj{public:Obj():val(newint(1)){}int&get(){return*val;}constint&get()const{return*val;}private:std::shared_ptrval;};正如预期的那样,当构造对象并制作拷贝时,它们都可以通过Obj公开的shared_ptr修改相同的值。Objnonconst1;Objnonconst2(nonconst1);nonconst2.get()=2;cout也可以从非const之一复制构造一个constObj对象,这似乎是正确的,因为它允许读取但不允许写入值-正如预期的那样以下

c++ - 如何通过 std::unique_ptr 成员调用其他类的 const 成员函数

在阅读有关向标准库添加const-propagating包装器的提案(文档编号N4388)时,我碰到了论文中给出的示例:#include#includestructA{voidbar()const{std::cout()){}voidfoo()const{std::coutbar();//callsA::bar()(non-const)//const_cast>(m_ptrA)->bar();//howtocalltheA::bar()const?}voidfoo(){std::coutbar();}std::unique_ptrm_ptrA;};intmain(){constBcon

c++ - 为什么 std::shared_ptr<T> = std::unique_ptr<T[]> 编译,而 std::shared_ptr<T[]> = std::unique_ptr<T[]> 不编译?

我使用以下输入命令在Coliru中探索了这个主题:g++-std=c++14-O2-Wall-pedantic-pthreadmain.cpp&&./a.out测试可以查到here,但我已经发布了下面的代码。我用了int在我的示例中,因为它是基本类型。#include#includestructFoo{Foo():a_{0},b_{1},c_{-1},combination_{0.5}{}inta_,b_,c_;doublecombination_;};intmain(){//int//*unManagedArray=newint[16];std::unique_ptruniqueAr

c++ - 添加从 unique_ptr<T> 到 T* 的隐式转换

一般问题:在不考虑这是否是一个好主意的情况下,如何将隐式转换运算符添加到已定义的类中?例如,假设我希望unique_ptr隐式转换为T*,但我不能只添加成员转换运算符,因为我不能更改unique_ptr类的定义。选项:是否有一些c++巫术可以让我在不创建成员函数的情况下实现这一点?迄今为止的答案:否。无法从无法在代码中修改的类型中添加隐式转换。只是……悲伤。我可以从std::unique_ptr派生并添加我自己的成员转换函数吗?这有什么严重的缺点吗?到目前为止的回答:是的(来自vsoftco)缺点尚未确定。到目前为止,从std::unique_ptr继承、继承其构造函数并声明隐式转换运