草庐IT

auto_cloned_unique_ptr

全部标签

C++智能指针之unique_ptr

C++智能指针之unique_ptr前言一、unique_ptr1.1unique_ptr类的初始化1.2unique_ptr禁止拷贝和赋值1.3release、reset函数1.4向unique_ptr传递删除器1.5unique_ptr与动态数组的使用总结前言  在C++中,动态内存的申请和释放是通过运算符:new和delete进行管理的。其中new负责申请内存,delete负责释放内存。  动态内存的使用很容易出现问题,这主要在于你需要保证在正确的时间释放内存,这是比较困难的,如果你忘记释放内存,就会造成内存泄露;有时在还有指针引用内存的情况下我们就释放了它,在这种情况下就会产生引用非法

c++ - std::map unique std::less<> 函数,用于 2D 点作为键

好吧,经过四个小时的调试,尽管我很困惑,但我找到了问题的原因......我正在制作一些程序,在std::map中保存一些点并在我的窗口中呈现这些点。但奇怪的是,有些点未能进入map。std::mapm_Props_m;voidAddProp(std::pairp){m_Props_m.insert(p);}structPoint2{unsignedintPoint2::x;unsignedintPoint2::y;//--------Point2::Point2():x(0),y(0){}boolPoint2::operator(constPoint2&b)const{return(x

c++ - 使用 shared_ptr 启动 std::thread

当你构造一个新线程时,提供的函数对象被复制到属于新创建线程的存储中。我想在一个新线程中执行一个对象方法。不应复制该对象。所以我将对象的shared_ptr传递给std::thread构造函数。如何使用std::shared_ptr()对象启动新线程?例如classFoo{public:voidoperator()(){//dosomething}};intmain(){std::shared_ptrfoo_ptr(newFoo);//Iwanttolaunchafoo_ptr()inanewthread//Isthisthecorrectway?std::threadmyThread(

c++ - 使 std::unique<T> 与 std::unique<const T, CustomDeleterType> 兼容

在代码中,我为特定对象定义了3个std::unique_ptr指针类型:typedefstd::unique_ptrnonConstPtrDefaultDelete;typedefstd::unique_ptr>nonConstPtrCustomDelete;typedefstd::unique_ptr>ConstPtrCustomDelete;我遇到了一个用例,我需要将nonConstPtrDefaultDelete转换为ConstPtrCustomDelete并将nonConstPtrCustomDelete转换为ConstPtrCustomDelete。换句话说:nonConst

c++ - 为什么我不能使用 auto 声明变量?

当我尝试声明一个类变量时,我在VisualStudio2015中遇到编译错误,而该类使用PIMPL模式。Foo.h:#pragmaonceclassFoo{public:Foo(conststd::wstring&str,conststd::vector&items);~Foo();private:structImpl;std::unique_ptrpimpl;};Foo.cpp:#include"stdafx.h"#include"Foo.h"structFoo::Impl{public:Impl(conststd::wstring&str,conststd::vector&item

c++ - 是否可以在 std::unique<T[ ]> 上应用 std::sort?

假设我有一个要排序的动态数组,我可以这样做std::vectorv(100);for(inti=0;i但对于性能关键代码,初始化开销是NotAcceptable,更多详细信息在https://stackoverflow.com/a/7269088/3667089我也可以int*v=newint[100];for(inti=0;i但是必须自己管理内存必然会导致大型代码库中的内存泄漏。所以看起来最可行的做法是std::unique_ptrv(newint[100]);for(inti=0;i没有初始化开销,也不需要担心内存管理,但这会返回一个很长的编译错误。有人可以让我知道我做错了什么吗?

c++ - 为什么我们需要 C++11 中的 weak_ptr?

我正在阅读NicolaiM.Josuttis的“TheC++StandardLibrary”一书以了解弱指针。作者提到了需要weak_ptr的两个原因,我不明白第二个原因。任何人都可以提供一个简单的解释以及以下原因的示例(引自书中):Anotherexampleoccurswhenyouexplicitlywanttosharebutnotownanobject.Thus,youhavethesemanticsthatthelifetimeofareferencetoanobjectoutlivestheobjectitrefersto.Here,shared_ptrswouldnev

c++ - 将这种原始指针情况变成 unique_ptr?

我有这样的代码:ISessionUpdater*updater=nullptr;if(eventName=="test")updater=newTestJSONSessionUpdater(doc);if(eventName=="plus")updater=newPlusJSONSessionUpdater(doc);if(updater){boolresult=updater->update(data);deleteupdater;returnresult;}returnfalse;除了使用unique_ptr之外,还有什么办法可以做这样的事情吗?也就是说,只有1次调用update(

c++ - 使 shared_ptr 失去内存所有权

我有一个shared_ptr我绕过它。最终,在某些情况下,我想将原始指针传递给一个函数,然后该函数成为内存所有者。在这些情况下shared_ptr不再负责释放内存,因为我调用的函数获得了所有权。我如何获得shared_ptr失去所有权?我想要shared_ptr的原因失去所有权是因为我想使用ProtocolBuffer的AddAllocated功能,它接受一个已经分配的指针并获得它的所有权。例子:shared_ptrmyProtoSharedPtr=//bythispointthisisthelastreferencetotheheapallocatedMyProto//Iwantto

具有抽象基类的 C++ boost::ptr_map 导致插入问题

在我的lastquestion之后我有一个抽象基类Action,它充当执行各种不同操作的接口(interface)。为了实现抽象层,我有一个ActionHandler类,其中存储各种Action:classActionHandler{public:ActionHandler();~ActionHandler();Action&getAction(std::stringActionString);private:boost::ptr_mapcmdmap;};我从对我之前问题的回答中了解到,boost会自动处理释放任何插入到该映射中的指针类型(类)。所以,我现在尝试插入从Action派生的