草庐IT

shared-object

全部标签

c++ - Qt 信号 lambda 导致 shared_ptr 泄漏?

我有以下代码:#include#include#include#includeclassDocument{public:Document(){qDebug("Document");}~Document(){qDebug("~Document");}QUndoStackmUndostack;};classDocumentRepository{public:DocumentRepository(){qDebug("DocumentRepository");}~DocumentRepository(){qDebug("~DocumentRepository");}voidAddDoc(std

c++ - 使用 shared_ptr 的内部指针的操作是原子的吗?

同时复制和重置shared_ptr是否安全?即考虑下面的代码//Mainthread(beforecreatinganyotherthreads)shared_ptra(newA(1));//Thread1shared_ptra_copy=a;//Thread2a.reset(new(A(2));其中线程1和2并行运行。我可以确定a_copy将存储指向较旧的A(1)或较新的A(2)共享对象的指针吗? 最佳答案 来自cppreference:Allmemberfunctions(includingcopyconstructorandc

c++ - shared_ptr 中对 const int 的 undefined reference

我有一个配置类//config.hppclassConfig{public:staticconstexprinta=1;staticconstexprintb=1;}并包含在main.cpp中//main.cpp#include"config.hpp"intmain(){std::coutstream=std::make_shared(Config::a);//compileerror}编译器说未定义对Config::a的引用它在使用cout时有效,但在shared_ptr构造函数中时无效。我不知道为什么会这样。 最佳答案 请注意s

c++ - 从 C++ 中同一个类的另一个成员函数中调用成员函数,Objective C

考虑以下几点:classA{//datamembersvoidfoo(){bar();//isthispossible?orshouldyousaythis->bar()notethatbarisnotstatic}voidbar(){}}//endofclassA如何从另一个内部调用成员函数?静态函数如何影响“this”的使用。应该在对象上调用函数吗? 最佳答案 Nawaz是正确的:'this'是隐含的。一个异常(exception)是如果foo是静态函数,因为在静态函数中没有“this”。在那种情况下,您不能使用bar()除非b

c++ - 将 googlemock EXPECT_CALL 与 shared_ptr 一起使用?

我有一个测试可以很好地使用原始指针,但我无法让它与std::shared_ptr一起工作。类是这样的:classMyClass{MyClass(SomeService*service);voidDoIt();}我的测试代码是这样的:classMyClassTests:public::testing::Test{public:MyClassTests():myClass_(newMyClass(&service_)){}protected:SomeServiceFakeservice_;MyClassSharedPointermyClass_;};TEST_F(MyClassTests,

c++ - decltype - "the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers"?

我正在阅读C++Primer,第5版,第1页。71他们首先给出了这个代码示例:constintci=0,&cj=ci;decltype(ci)x=0;decltype(cj)y=x;decltype(cj)z;//error然后他们说:Itisworthnotingthatdecltypeistheonlycontextinwhichavariabledefinedasareferenceisnottreatedasasynonymfortheobjecttowhichitrefers.这是什么意思?我不明白。y指的是x。那么有什么收获呢? 最佳答案

c++ - 在 shared_ptr 容器中查找元素?

我在查找shared_ptrvector中的元素时遇到了一点问题。这是我最终得到的结果:std::vector>blocks;boolcontains(Block*block){for(autoi=blocks.begin();i!=blocks.end();++i){if((*i).get()==block){returntrue;}}returnfalse;}但是,我没能用std::find甚至std::find_if做到这一点。是否有更符合C++标准的方法来实现这一目标?编辑:这是我在回答之后的代码:boolcontains(Block*block){autofound=std:

c++ - shared_ptr 析构函数、复制和不完整类型

我有一个头文件foo.h像这样(省略不相关的东西):#pragmaonce#includeclassBar;structFoo{std::shared_ptrgetBar();std::shared_ptrgetBar()const{returnconst_cast(this)->getBar();}};getBar()的非常量重载在.cpp文件中实现,该文件还可以看到Bar的完整定义.当foo.h包含在另一个文件中(没有看到Bar的定义),VS2010给我这样的警告:warningC4150:deletionofpointertoincompletetype'Bar';nodestr

c++ - Make_shared - 自己的实现

我正在尝试自己实现shared_ptr。make_shared有问题。std::make_shared的主要特点是它在连续的内存块中分配计数器block和对象。我怎样才能做同样的事情?我试过这样做:templateclassshared_ptr{private:class_ref_cntr{private:longcounter;public:_ref_cntr():counter(1){}voidinc(){++counter;}voiddec(){if(counter==0){throwstd::logic_error("alreadyzero");}--counter;}long

c++ - 尝试执行 shared_ptr swap() 时出现奇怪错误

我是一个相对的C++新手,试图将一个现有项目从原始指针转换为使用C++11的shared_ptr.总的来说进展非常顺利,我认为我理解如何shared_ptr在移动语义、右值引用等方面工作。好东西。但是,我遇到了一个奇怪的错误,我不明白也不知道如何修复。先介绍一点背景。我有一个根植于名为EidosValue的抽象基类的类层次结构,和一个名为EidosValue_Int_vector的类是(间接地)一个具体的子类:classEidosValueclassEidosValue_Int:publicEidosValueclassEidosValue_Int_vector:publicEidos