草庐IT

auto_ptr

全部标签

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++ - foreach(int i.. 和 foreach(auto i

我正在MacOX(LLVM4.2)附带的Clang编译器上试验C++11功能,以下结果让我感到困惑://clangcompilewith"c++-std=c++11-stdlib=libc++"#include#includeintmain(void){usingnamespacestd;vectoralist={1,2,3,4};for(inti=0;i在运行环境中,我得到如下不同的输出:12342340为什么我会得到不同的结果? 最佳答案 for(autoi:alist)这会获取alist中的每个value,因此i变为:1,2,

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++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案