草庐IT

c++ - 为什么智能指针 vector 不是指向实现与该接口(interface)协变的接口(interface)的项目?

为什么智能指针vector不与item实现的接口(interface)协变?例如如果我有一个指向狗的指针vector,为什么我不能将它用作指向iAnimal的指针vector?#include#include#include#includestructiAnimal{virtualstd::stringspeak()const=0;};structiMammal:publiciAnimal{virtualstd::stringspeak()const=0;virtualintlegs()const=0;};structDog:publiciMammal{std::stringspeak

c++ - 在包含 shared_ptr 的 map 上使用 find_if 会增加引用计数

我正在创建一个程序,它有一个包含shared_ptr的映射。当我尝试使用std::find_if在其中查找元素时,shared_ptr的引用计数会增加。示例:#include#include#include#includeintmain(void){std::map>map;map[1]=std::make_shared(3);map[2]=std::make_shared(5);map[4]=std::make_shared(-2);autoit=std::find_if(map.begin(),map.end(),[](conststd::pair>&elem){std::cout

c++ - 在 GCC 中使用 shared_ptr 的可移植方式

GCC4.1使用header和GCC4.3使用header,我需要一种可移植的方式来使用shared_ptr使用GCC4.3.2和GCC4.2.1,有没有办法在不检查GCC版本宏或使用外部库(如Boost)的情况下做到这一点? 最佳答案 仍将与gcc4.3一起工作。如果您想同时支持这两个版本,只需使用tr1姓名。 关于c++-在GCC中使用shared_ptr的可移植方式,我们在StackOverflow上找到一个类似的问题: https://stackove

c++ - CreateFile 失败并出现错误 ERROR_SHARING_VIOLATION

我使用CreateFileapi,有时它随机失败并出现错误:ERROR_SHARING_VIOLATION。我用谷歌搜索了一下,几乎没有关于这个错误的信息。奇怪的是下次打开同一个文件还是蛮开心的。这是我的代码:voidFileHandle::open(constchar*fileName,FILE_MODEmode){if(m_bIsOpen)close();HANDLEfh=NULL;DWORDdwDesiredAccess=GENERIC_READ;DWORDdwShareMode=FILE_SHARE_READ;DWORDdwCreationDisposition=OPEN_EX

c++ - 为什么 shared_ptr<void> 而不是 shared_ptr<HANDLE>

基于http://en.highscore.de/cpp/boost/smartpointers.html#smartpointers_shared_pointer#include#includeintmain(){boost::shared_ptrh(OpenProcess(PROCESS_SET_INFORMATION,FALSE,GetCurrentProcessId()),CloseHandle);SetPriorityClass(h.get(),HIGH_PRIORITY_CLASS);}问题:为什么h定义为boost::shared_ptr而不是boost::shared_

c++ - std::find 跨一组 shared_ptr

我确定我在这里做了一些愚蠢的事情,但我看不到它。为什么不能编译以下内容?#include#include#include#include//Aclasstoplaywith.Encapsulatesaname.classStringClass{public:StringClass(std::stringconst&name):MyName(name){}std::stringconst&Name()const{returnMyName;}private:std::stringMyName;};//Thesetofinstancesof"StringClass".std::vector>

c++ - std::static_pointer_cast 是否有任何额外的运行时开销?

相对于static_cast,即。所以,如果我们有这两个类型转换Base*b(newDerived());Derived*d=static_cast(b);//(1)shared_ptrb(newDerived());shared_ptrd=static_pointer_cast(b);//(2)第(2)行会比第(1)行慢吗? 最佳答案 是的,它有更多的开销,因为它必须返回一个新的shared_ptr而不是一个新的原始指针。boost实现是:templateshared_ptrstatic_pointer_cast(shared_p

c++ - 为什么使用 shared_ptr 创建弱指针?

我写了以下简单的代码,#include#includestructFoo{Foo(){std::coutp1(newFoo);//thislinep1->bar();std::shared_ptrp2(p1);}然后在监window口中,我得到了,p2std::__1::shared_ptrptr=0x100104350strong=2weak=1p1std::__1::shared_ptrptr=0x100104350strong=2weak=1现在,我能理解strong=2,但为什么weak=1呢?其次,在代码中我将代码更改为,std::shared_ptrp1(std::move

c++ - 在公共(public)接口(interface)中使用共享指针

我们有一个使用共享指针的非常标准的树API,大致如下所示(为简洁起见省略了实现):classnode;usingnode_ptr=std::shared_ptr;classnode:publicstd::enable_shared_from_this{std::weak_ptrparent;std::vectorchildren;public:virtual~node()=default;virtualvoiddo_something()=0;voidadd_child(node_ptrnew_child);voidremove_child(node_ptrchild);node_pt

c++ - 如果在 user.hpp 中定义了 BOOST_NO_EXCEPTIONS,为什么不能编译 boost::shared_ptr

我有一个嵌入式系统,想在这个系统中使用boost,但需要禁用异常,因为我不想支付异常成本。boost给了一个user.hpp和可设置的宏选项BOOST_NO_EXCEPTIONS和BOOST_NO_EXCEPTION_STD_NAMESPACE,但是boost::shared_ptr不能编译(更准确的说,不能链接)如果定义了这两个宏。shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a):undefinedrefe