草庐IT

c++ - shared_ptr 的隐式转换

我有两个类U和T的shared_ptr,其中T是U的基数。从shared_ptr做一个隐式转换是没有问题的至shared_ptr.但也可以从shared_ptr进行转换至shared_ptr?我尝试了建议的解决方案:classT{public:virtual~T(){}protected:voidfillData()=0;};classTimpl:publicT{public:virtual~Timpl(){}protected:virtualvoidfillData()=0;};classU:publicTimpl{public:virtual~U(){}protected:virt

c++ - 将 std::shared_ptr 与指向成员函数的指针一起使用

使用C指针我可以做这样的事情:#include#includeclassFoo{voidbar(){std::coutfoo_sptr(&foo);void(Foo::*bar_ptr)()=&Foo::bar;(foo.*bar_ptr)();(foo_ptr->*bar_ptr)();//(foo_sptr->*bar_ptr)();//doesnotcompileforme如果我想使用smart_ptr而不是C指针,我会得到一个编译器错误:error:nooperator"->*"matchestheseoperandsoperandtypesare:std::shared_pt

c++ - 使用 std::move 和 std::shared_ptr 有什么好处和风险(如果有的话)

我正在学习C++11的特性,作为其中的一部分,我首先进入了unique_ptr和shared_ptr的世界。开始时,我编写了一些专门使用unique_ptr的代码,因此当我传递我的变量时,我需要使用std::move来完成它(或者让我明白)。经过一番努力,我意识到我确实需要shared_ptr而不是我正在做的事情。稍后快速查找/替换,我的指针被切换到共享,但我懒洋洋地只是留下了move()调用。令我惊讶的是,它不仅可以编译,而且在我的程序中表现得非常好,我得到了我期望的每一点功能......特别是,我能够“move”一个shared_ptr从ObjectA到ObjectB,并且两个对象

c++ - 我的 shared_ptr 声明中有什么错误?

我有一个类持有另一个类的shared_ptr。shared_ptr声明出现编译错误,提示“没有使用此类型定义的成员”。我复制这个的代码很短:#include#includeclassMyClassImpl{};classMyClass{public:boost::shared_ptr_sptr;//errorC2208:'boost::shared_ptr':nomembersdefinedusingthistype};intmain(){MyClassmc;return0;}我在这里做错了什么?我是装有Boost1.54的VisualStudioProfessional2010。

c++ - 为具有 shared_ptr 数据成员的类复制构造函数?

我知道当类中有原始指针数据成员时如何编写复制构造函数,但是当使用shared_ptr管理它们时如何编写复制构造函数?是否有应该调用的copy()或clone()函数?有趣的是,我从未见过这样的例子。 最佳答案 std::shared_ptr的拷贝构造函数创建与第一个指针共享所有权的第二个指针。当所有std::shared_ptr时,指针将被销毁指向它的那一点被摧毁。如果没有显式声明复制构造函数,语言规范会规定将隐式提供合适的复制构造函数,并且它将调用类中每个成员的复制构造函数。因此,换句话说,为您的类提供的隐式复制构造函数将调用sh

c++ - 涉及 const unique_ptr 的 move 构造函数

在下面的代码中,我将p设置为const,因为它在Foo的生命周期内永远不会指向任何其他int。这不会编译,因为调用了unique_ptr的复制构造函数,这显然已被删除。除了使p非常量之外还有什么解决方案吗?谢谢。#includeusingnamespacestd;classFoo{public://xisalargestructinrealityFoo(constint*constx):p(x){};Foo(Foo&&foo):p(std::move(foo.p)){};private:constunique_ptrp;}; 最佳答案

c++ - 如何将非静态成员函数作为 unique_ptr 删除器传递

这个问题在这里已经有了答案:HowCanIPassaMemberFunctiontoaFunctionPointer?(3个答案)关闭7年前。#include#include#include#includeclassclient{private:std::unique_ptruptr_curl_;inlineCURL*init_curl(){CURLcoderesult=curl_global_init(CURL_GLOBAL_DEFAULT);if(result!=CURLE_OK)throwstd::logic_error(curl_easy_strerror(result));r

c++ - 从工厂函数返回 std::unique_ptr<T> 创建纯虚拟接口(interface)的完全隐藏实现

我正在阅读SmartPointerProgrammingTechniques在boost文档中提供。在“usingabstractclassesforimplementationhiding”部分,他们提供了一个很好的习惯用法来完全隐藏纯虚拟接口(interface)背后的实现。例如://Foo.hpp#includeclassFoo{public:virtualvoidExecute()const=0;protected:~Foo()=default;};std::shared_ptrMakeFoo();和//Foo.cpp#include"Foo.hpp"#includeclass

c++ - 无法将 unique_ptr 添加到 std::array

在书籍和网上搜索之后,直到我的耳朵之间开始剧烈疼痛,我无法弄清楚如何将std::unique_ptr添加到std::array.以下是类成员:std::array,MAX_ELMS_NUM>m_collection;在.cpp文件中,我试图将填充在std::unique_ptr中的新媒体指针添加到数组中:Media*newMedia=CreateNewMedia(Mediainfostuff);unique_ptrnp(newMedia);m_collection[0]=np;除了最后一行之外的所有内容都可以编译。 最佳答案 最后一

c++ - map 中过期的 weak_ptr 会发生什么

我想了解weak_ptr已过期的映射中的条目(类型为boost::weak_ptr)会发生什么。map中的相应条目是否会自动删除?键是一个整数,对应的值是一个weak_ptr。我写的示例代码,但无法编译#include#include#includeusingnamespacestd;classFoo:publicboost::enable_shared_from_this{public:Foo(intn=0):bar(n){std::coutinc_ref(){returnshared_from_this();}private:intbar;};std::map>mappy;intm