草庐IT

HTB靶场 Shared

全部标签

C++ 什么是 std::shared_future 和 std::promise

我了解什么是std::future但我不了解何时以及如何使用std::shared_future和std::promise,而且我在网上找不到很好的解释。我会很感激一些帮助我解决这个问题。顺便说一句,这里的例子不是很清楚http://en.cppreference.com/w/cpp/thread/shared_future 最佳答案 std::shared_future当您需要拥有std::future的多个有效拷贝时,并且可能是上述std::future的多个消费者.您可以移动std::future进入std::shared_f

c++ - c++ shared_ptr中的相互破坏

大家。我在C++解构中发现了一个问题。我将在下面向您展示我的代码:#include#include#includeusingnamespacestd;classB;classA{public:typedefshared_ptrPtr;shared_ptrb;intindex;A(constshared_ptr&b_,intindex_):b(b_),index(index_){}~A(){coutPtr;vectorall_a;voidaddA(){for(inti=0;i(Ptr(this),i);all_a.push_back(a);}}intindex;B(intindex_):

c++ - 将从唯一指针移动到共享指针也会初始化 enable_shared_from_this

当我继承std::enable_shared_from_this,但是我创建了一个unique_ptr,std::enable_shared_from_this里面的weak_ptr也会被初始化吗当我通过std::move或移动构造函数“移动”到shared_ptr时?例如下面的代码会发生什么:#include#includeclassA:publicstd::enable_shared_from_this{public:std::shared_ptrgetA(){returnshared_from_this();}};intmain(){std::unique_ptru(newA()

c++ - 原始指针参数的 shared_ptr

当函数需要一个char*时,可以传入一个shared_ptr吗?我正在读取整个文本文件(长度=100),并希望将char存储到char[]数组中。我使用的天真方式是这样的:ifstreamdictFile(fileName);size_tfileLength=100;char*readInBuffer(newchar[fileLength]);dictFile.read(readInBuffer,fileLength);//processingreadInBuffuer..............delete[]readInBuffer;dictFile.close();当然,如果在d

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++ - 使用 new 运算符定义 std::shared_ptr 时出错

我正在尝试通过以下方式使用new运算符定义std::shared_ptr:#includestructA{};intmain(){std::shared_ptrptr=newA();return0;}但我得到了以下编译时错误:main.cpp:Infunction'intmain()':main.cpp:8:30:error:conversionfrom'A*'tonon-scalartype'std::shared_ptr'requestedstd::shared_ptrptr=newA();无论如何,以下绝对有效:std::shared_ptrptr{newA()};有谁知道为什么