草庐IT

C++ 删除了 new 运算符但可以创建 shared_ptr

我试图寻找答案,但找不到任何有助于理解幕后发生的事情。假设我们有以下内容:classBase{public:virtualintrequest()=0;void*operatornew(longunsignedintsize)=delete;};classDerivedfinal:publicBase{public:intrequest()override;};intDerived::request(){return2;}在基类上,我删除了new运算符,因为我不想创建指针。我知道这可能没有任何意义,这只是一个实验。当我调用g++时,出现编译错误,告诉我新运算符已被删除。正是我所期望的。

c++ - 将 std::unique_ptr 与 lambda 交换为删除器——GCC

我们可以使用lambda作为带有std::unique_ptr的删除器吗?实际上,我是用clang++做的,它很高兴这样做。我正在使用std::swap换成std::unique_ptr;其中autodeleter=[](structaddrinfo*ptr){if(ptr!=nullptr){freeaddrinfo(ptr);}};.Clang的交换似乎不需要复制赋值运算符,但gcc的std::swap需要,正如您在这些日志中看到的那样:Infileincludedfrom/usr/include/c++/4.8.1/memory:81:0,from/home/zenol/proj

c++ - void (**vt)() = *(void (***)())ptr; c++中虚拟表的辅助变量

我在以下链接中找到了这项技术:http://www.codeproject.com/Tips/90875/Displaying-vtable-when-debugging在那里,他使用了一个辅助变量void(**vt)()=*(void(***)())ptr;帮助显示虚函数表。但是如果我把它改成void(**vt)()=(void(**)())ptr;它不像以前那样工作。有人能帮我解释一下这里的魔法吗? 最佳答案 为了清楚起见,让我们引入一个typedef。typedefvoid(**thing)();那么第一个代码是thingvt

c++ - Lambda 捕获 shared_ptr 成员

我有一个类OpenGLRenderer它有一个类(class)成员mMemoryAllocator那是一个std::shared_ptr.我将内存分配器保留在shared_ptr中的原因是因为即使shared_ptr下面返回的时间超过了它的创建者OpenGLRenderer,MemoryAllocator如果我按值捕获实例,实例仍然有效,因为它会增加引用计数:std::shared_ptrOpenGLRenderer::CreateTexture(TextureTypetextureType,conststd::vector&textureData,uint32_ttextureWid

c++ - shared_ptr 和 this 指针

好的,我开始使用共享指针并尽可能多地传递共享指针。不再转换为原始指针。这很好用,除了在这种特定情况下:假设我们有一个类也是另一个类的观察者,如下所示:classMyClass:publicIObserver{public:MyClass(std::shared_ptrotherClass);voidDoSomethingImportant();private:std::shared_ptrm_otherClass;};这个类在我的应用中是这样使用的:std::shared_ptrmyInstance(newMyClass(otherInstance));...myInstance->D

C++ Lambda、捕获、智能 Ptr 和堆栈 : Why Does this Work?

我一直在研究C++11中的一些新特性,并尝试编写以下程序,但预计它不会运行。令我惊讶的是,它确实如此(在带有'std=c++0x'标志的Linuxx86上的GCC4.6.1上):#include#include#includestd::functioncount_up_in_2s(constintfrom){std::shared_ptrfrom_ref(newint(from));return[from_ref](){return*from_ref+=2;};}intmain(){autoiter_1=count_up_in_2s(5);autoiter_2=count_up_in_

c++ - Clang、std::shared_ptr 和 std::less/operator<

有如下代码#includeintmain(){std::shared_ptrptr0(newint);std::shared_ptrptr1(newint);boolresult=ptr0在用clang(version3.1,LLVM3.1,DebianGNU/LinuxSid)编译时产生以下错误/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/shared_ptr.h:364:14:error:nomatchingfunctionforcalltoobjectoftype'std::le

c++ - 将具有 unique_ptr 参数的函数绑定(bind)到 std::function<void()>

我正在尝试使以下代码工作:#include#include#include#includeusingnamespacestd;classFoo{public:Foo():m_str("foo"){}voidf1(strings1,strings2,unique_ptrp){printf("1:%s%s%s\n",s1.c_str(),s2.c_str(),p->str());}voidf2(strings1,strings2,Foo*p){printf("2:%s%s%s\n",s1.c_str(),s2.c_str(),p->str());}constchar*str()const{

c++ - 一旦对 std::weak_ptr 的锁定返回了 nullptr,它还能再次为非空吗?

如果你锁定std::weak_ptr:classfoo{};autos=std::make_shared();std::weak_ptrw{s};s=nullptr;s.reset(newfoo());autol=w.lock();//trytogetpreviousfoostd::cout输出:locked:0一旦lock返回nullptr,是否存在它可以返回非null的条件,或者它实际上已经死了?我的测试程序建议,一旦最初分配的对象的引用计数为零,则不会,weak_ptr将始终返回nullptr。 最佳答案 isthereeve

c++ - 正确使用shared_ptr?

没有使用shared_ptr的经验我想知道以下是否是合适的用例,以及返回shared_ptr是否是个好主意给用户。我有一个类似图形的结构,节点之间有多个连接。在图的遍历过程中,每个节点都被分配了一个值(从连接的节点计算),我希望用户能够轻松访问该值。整个事情看起来(强烈简化)如下:classPool;classNode{public:typedefstd::tr1::shared_ptrPtr;...voidcompute_dependencies(){...//callsPooltogetanewNodeinstancedependencies_.push_back(Pool::cr