草庐IT

c++ - 具有 unique_ptr 的用户友好型 API

我正在为OpenGL实现一个简单的GUI,主要是作为我自己的练习。这个想法是有一个Gui类,其中每个实例都可以分配给不同的渲染目标(例如后台缓冲区或纹理)。GUI元素(小部件)恰好分配给Gui类的一个实例。我想在GUI中存储元素是unique_ptr的典型用例.这是我想出的:classElement{public:Element();virtual~Element();staticstd::unique_ptrcreate_unique();};classGui{public:typedefstd::unique_ptrelement_ptr;Gui();voidaddElement(

c++ std::unique_ptr 不会在 map 中编译

我目前正在尝试将std::unique_ptr存储在std::unordered_map中,但出现奇怪的编译错误。相关代码:#pragmaonce#include"Entity.h"#include#includeclassEntityManager{private:typedefstd::unique_ptrEntityPtr;typedefstd::mapEntityMap;EntityMapmap;public:/*AddsanEntity*/voidaddEntity(EntityPtr);/*RemovesanEntitybyitsID*/voidremoveEntity(i

c++ - 如何编写指针和数据为 const 的 unique_ptr

我第一次尝试编写一个基于范围的for循环来遍历unique_ptr时,我写道:std::vector>vec;//Initializevecfor(autov:vec)//error{}然后我意识到这是在尝试创建每个元素的拷贝,这对unique_ptr没有意义。所以后来我写了它作为引用:for(auto&v:vec){}在它前面添加一个const可以防止我更改指针。for(constauto&v:vec){v=nullptr;//error(good!)}要怎么写,才能让指向的数据不能改变?例如,以下代码不应编译。for(???v:vec){v->func();}classFoo{pu

c++ - std::unique_ptr 和 std::shared_ptr 之间破坏行为差异的基本原理是什么?

来自http://en.cppreference.com/w/cpp/memory/unique_ptr:IfTisderivedclass(sic)ofsomebaseB,thenstd::unique_ptrisimplicitlyconvertibletostd::unique_ptr.Thedefaultdeleteroftheresultingstd::unique_ptrwilluseoperatordeleteforB,leadingtoundefinedbehaviorunlessthedestructorofBisvirtual.Notethatstd::shared

c++ - unique_ptr、自定义删除器和零规则

我正在编写一个类,该类使用两个通过C接口(interface)创建的对象。对象看起来像:typedefstruct...foo_t;foo_t*create_foo(int,double,whatever);voiddelete_foo(foo_t*);(与bar_t类似)。因为C++11,我想将它们包装在一个智能指针中,这样我就不必编写任何特殊方法。该类将拥有这两个对象的唯一所有权,因此unique_ptr在逻辑上是有意义的......但我仍然必须编写一个构造函数:templateusingunique_ptr_deleter=std::unique_ptr;structMyClas

c++ - 没有 RTTI 的 shared_ptr?

我正在尝试在使用xc321.34(gcc4.5.2的衍生物)构建的嵌入式项目中使用shared_ptr。该项目使用-fno-rtti禁用了RTTI。#include仅包含header会出现以下错误:/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory:Inmemberfunction'virtualvoid*std::tr1::_Ref_count_del::_Get_deleter(conststd::type_info

c++ - 当在类主体中使用 unique_ptr 声明析构函数作为同一类的成员时出现编译器错误

下面是代码的极简问题:structB{B()=default;//~B(){};//error:useofdeletedfunction‘B&B::operator=(constB&)’std::unique_ptrm_pB=nullptr;};intmain(){std::vectorvB;vB.erase(vB.begin());}除非取消注释析构函数,否则以上代码可以正常编译。根据我的要求,我需要一个~B()的正文明确定义。如何使用unique_ptr定义析构函数的主体?同类共存?注意:尝试定义=default复制和移动构造函数的版本无济于事。在我的真实代码中,unique_pt

c++ - 使用 shared_ptr 和 weak_ptr 时避免间接循环引用

我目前正在组装一个严重依赖shared_ptr的应用程序,到目前为止一切看起来都很好-我已经完成了我的homework并且非常清楚使用shared_ptr的一些陷阱shared_ptr最常见的问题之一是循环依赖-这些问题可以通过存储weak_ptr来解决,这些weak_ptr不会影响上链对象的生命周期.但是,我很难理解需要通过weak_ptr存储指向外部对象的指针的时间-我不确定它是否被禁止、不鼓励,或者是否这是安全的。下图描述了我的意思(黑色箭头表示shared_ptr;虚线表示weak_ptr):alttexthttp://img694.imageshack.us/img694/6

c++ - 为什么我不能在 Exception 类中有 auto_ptr

我在异常类中遇到了auto_ptr的问题,我最终将其简化为:#includeclassMyException{std::auto_ptrm_foo2;};intmain(){try{throwMyException();}catch(constMyException&){}return0;}编译失败:/perforce/unstable/test/Common/Exceptions/TestException4.cpp:Infunction'intmain()':/perforce/unstable/test/Common/Exceptions/TestException4.cpp:1

c++ - 将 ptr 显式转换为 "ptr to a ptr"

我在一次采访中看到了这段代码。intmain(){int**p;p=(int**)newint(7);cout我原以为*p会出现一些运行时错误。但是当我运行代码时,它成功执行并输出“0x7”。有人可以向我解释这是如何工作的吗?谢谢。 最佳答案 正确的答案是以上都不是,除非给你一些额外的限制。基本上,代码分配一个int并将该内存解释为一个int*(通过reinterpret_cast)。第一个问题是,作为一个reinterpret_cast,结果在一般情况下是unspecified,如果int的大小小于int*的大小(考虑64位架构)