草庐IT

intrusive_ptr

全部标签

从基础到派生的 shared_ptr 的 C++ dynamic_ptr_cast 失败

这是我本周遇到的一个益智游戏。部分原因是我在编写了一段时间的Java代码后刚刚回到C++编码。给定以下代码:classBase{};classA:Base{public:virtualvoidrun(){coutptrToA=shared_ptr(newC());cout(ptrToA)run();assert(dynamic_pointer_cast(ptrToA));cout为什么会产生如下输出?PointertoA:0x1f29c010DynamicCastAptrtoC:0Running...ThisisC.tester-cpp:tester.cpp:89:intmain(in

c++ - std::unique_ptr<T> 不完整类型错误

我有templateclassqueue{private:structnode{Tdata;std::unique_ptrnext;//compileerroronincompletetypenode(T&&data_):data(std::move(data_)){}};std::unique_ptrhead;node*tail;public:queue():tail(nullptr){}我在VS10的标记行上遇到编译错误。在这种情况下,我是否应该被允许使用不完整的类型(实例化模板-构造函数-这里以int为例)?有解决方法吗?编辑singlethreadedqueue.h(62):e

c++ - boost shared_ptr的底层设计

我想了解boostshared_ptr类的底层设计。我想将它“移植”到fortran(不要问)。我理解的一件事是引用计数由shared_count类持有。这提示了我一个问题。很久没用过C++了,也没用过boost。假设我分配了一个类X的实例,然后将它传递给两个不同的shared_ptr实例。据我了解,每个shared_ptr实例对另一个实例一无所知,因此两个shared_ptr实例都引用同一个X实例,同时保持引用计数为1。如果一个shared_ptr超出范围而另一个不超出范围,则X对象将被删除(因为引用计数降为零)并且剩余的shared_ptr将有一个悬空指针。为了保持shared_p

c++ - vector<shared_ptr<T>> 和 vector<shared_ptr<const T>> 之间的高效转换

我有一个类:classX{vector>v_;public:vector>getTs(){returnv_;}};它有一个vector的shared_ptr类型T.出于某种原因,它需要公开一个方法来返回这个vector。但是,我不想修改vector的内容,也不想指向对象。所以我需要返回一个vector的shared_ptr.我的问题是,有什么有效的方法可以做到这一点吗?如果我简单地返回它,它可以工作,但它需要重建一个vector,这有点昂贵。谢谢。 最佳答案 你不能直接这样做-但你可以在你的容器上定义“View”,让你做一些非常相似

c++ - 函数中auto_ptr的返回值

我遇到过这样的代码。MyClassMyClass::get_information(constsome_datastructure*record){auto_ptrvariable(newMyClass());variable->set_article_id(record->article_id);return*variable.get();}我知道这会返回一个(拷贝?)MyClass类型的对象。最初,我认为它正在返回对我来说没有意义的auto_ptr对象(?)因为我认为auto_ptr对象在超出范围时会被销毁。无论如何,上面的代码可以吗?对象*variable.get()在函数返回时

c++ - 为什么这个 time_zone_ptr 示例不包含内存泄漏?

我正在阅读boostDateTime库here,其中包含许多示例,例如:time_zone_ptrzone(newposix_time_zone("MST-07"));我很好奇为什么使用关键字“new”不会导致内存泄漏?我调查了boost源代码,注意到它有两个不同版本的构造函数,一个使用shared_ptr,另一个使用weak_ptr。有人可以解释这些是如何工作的,以及为什么上面的行可以安全编写? 最佳答案 time_zone_ptr只是boost::shared_ptr的别名.这是一个智能指针,它获取动态分配对象的所有权,从构造它

c++ - 在顶层使用 shared_ptr 而不是 scoped_ptr 有什么优势吗?

我的团队对于指针容器在特定上下文中的使用存在一些分歧。请考虑:intmain(){//Toplevel.Thisisanimportantfacttothecontext//i.e.thatthefollowinginstanceisatthislevel//sothatitsmembersareessentiallyatprogramscope.MainClassmainClass;mainClass.run();}//AinstanceofaclassderivedfromBufferdoessomethingverycomplex//(ithasvarioushandlestor

c++ - 具有多态类型的 unique_ptr 未被删除

我有一个使用基类派生类型存储的unique_ptrvectorstd::unique_ptr>>decisionVariables;其中Variable是父类(superclass),派生类型是Route类。我的问题是,当包含decisionVariables的类被删除时,路由实例似乎没有被删除。路由来源于变量:#ifndef__VARIABLE__#define__VARIABLE__/***Interfacefordecisionvariables.*/#include#include#includeclassVariable{public:/***Returnsanindepen

c++ - 基于模板参数是否为 shared_ptr 的函数特化

我正在尝试检测类型是否为shared_ptr如果是,则分派(dispatch)到特定函数模板或覆盖。这是我实际尝试的简化版本:#include#include#includetemplatestructis_shared_ptr:std::false_type{};templatestructis_shared_ptr>:std::true_type{};classFoo{};typedefstd::shared_ptrSharedFoo;templatevoidgetValue();template::value>::type=0>voidgetValue(){printf("sha

c++ - 使用 make_shared<std::thread> 创建 shared_ptr<std::thread> 的实例

考虑以下代码:classA{....shared_ptrmThread;voidStep();voidLaunchTrhead();}voidA::LaunchThread(){...mThread=make_shared(Step);//Thislinegivesanerror...}voidA::Step(){...}我正在尝试初始化共享指针mThread以便它调用函数Step。但是,编译器给我错误“类型引用的无效初始化...来自类型‘未解析的重载函数类型’的表达式”。显然我在做一些愚蠢的事情,但我不能指责它。有人可以帮忙吗?提前致谢! 最佳答案