草庐IT

gray8_ptr_t

全部标签

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>

c++ - 通过 std::unique_ptr 扩展变量的生命周期

使用C++11unique_ptr,对象的生命周期似乎被延长到其通常范围之外,如下面(相当人为的)示例所示:#include#includeusingnamespacestd;intmain(){unique_ptruPtr(nullptr);{charc='X';cout输出:c=Xc=Y通常在作用域结束时释放的字符c一直存在到程序结束。第二个输出是“Y”,表明unique_ptr不只是简单地复制它的值。是否建议以某种方式延长变量的生命周期?这是否安全,或者它是否具有与引用相同的危险? 最佳答案 WiththeC++11uniqu

C++11 通过原始指针或引用获取 unique_ptr 的所有权?

编辑我想下面的代码会假设我有一个addChild()的重载版本,它接受一个已经包装在unique_ptr中的Sprite,在那里取得所有权就可以了。只是想我会在其他人之前提到这一点。:)。经过漫长的一天,我在这里编写了所有代码,因此请将其视为伪代码质量,仅用于演示手头的问题。原始问题我正在编写一个框架,其中有一个显示列表、parent/child等。我认为使用unique_ptrforexample是这里的方法,因为当您将子项添加到父显示对象时,父项现在成为该子项的唯一所有者是合乎逻辑的。但是,将有可用的方法,例如getChildAt(index)和getChildByName等,我认

c++ - 为什么使用 shared_ptr 创建弱指针?

我写了以下简单的代码,#include#includestructFoo{Foo(){std::coutp1(newFoo);//thislinep1->bar();std::shared_ptrp2(p1);}然后在监window口中,我得到了,p2std::__1::shared_ptrptr=0x100104350strong=2weak=1p1std::__1::shared_ptrptr=0x100104350strong=2weak=1现在,我能理解strong=2,但为什么weak=1呢?其次,在代码中我将代码更改为,std::shared_ptrp1(std::move

c++ - 为什么 unique_ptr<T>::~unique_ptr 需要 T 的定义?

如果我有一个“酒吧”类://bar.hclassBar{public:Bar(){}};我转发声明与另一个类“Foo”中的std::unique_ptr一起使用://foo.h#includeclassBar;classFoo{public:Foo();private:std::unique_ptrbar_;};我在Foo的实现文件中包含了它的定义://foo.cpp#include"foo.h"#include"bar.h"Foo::Foo():bar_(newBar){}我收到编译时错误“‘sizeof’对不完整类型‘Bar’的无效应用”。我从here了解到和here为了解决这个问

c++ - 如何将 unique_ptr 移出 vector<unique_ptr<Foo>>?

我想搬一个unique_ptr从一个vector>.考虑我的代码:#include#include#includeusingnamespacestd;classFoo{public:intx;Foo(intx):x(x){};~Foo(){cout>();foos.push_back(unique_ptr(newFoo(100)));foos.push_back(unique_ptr(newFoo(101)));foos.push_back(unique_ptr(newFoo(102)));//Printallcoutxxx我预计它会产生:Vectorsize:3100101102Re

c++ - 在递归数据结构中 move unique_ptr<T> 数组

尝试编译以下代码会导致以下编译错误:errorC2280:'std::unique_ptr>::unique_ptr(conststd::unique_ptr>&)':attemptingtoreferenceadeletedfunction我的理解是数组“m_children”应该是可move的,因为unique_ptr指向的类型定义了move构造函数。除非这是由类的递归性质或我忽略的某些move语义元素引起的错误?#include#include#includeclassOctreeNode{public:OctreeNode(){};OctreeNode(OctreeNode&&

c++ - 如果在 user.hpp 中定义了 BOOST_NO_EXCEPTIONS,为什么不能编译 boost::shared_ptr

我有一个嵌入式系统,想在这个系统中使用boost,但需要禁用异常,因为我不想支付异常成本。boost给了一个user.hpp和可设置的宏选项BOOST_NO_EXCEPTIONS和BOOST_NO_EXCEPTION_STD_NAMESPACE,但是boost::shared_ptr不能编译(更准确的说,不能链接)如果定义了这两个宏。shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a):undefinedrefe