草庐IT

weak_ptr_cast

全部标签

c++ - 为什么 shared_ptr 需要保存 weak_ptr 的引用计数?

引自C++Primer$12.1.6:Aweak_ptr(Table12.5)isasmartpointerthatdoesnotcontrolthelifetimeoftheobjecttowhichitpoints.Instead,aweak_ptrpointstoanobjectthatismanagedbyashared_ptr.Bindingaweak_ptrtoashared_ptrdoesnotchangethereferencecountofthatshared_ptr.Oncethelastshared_ptrpointingtotheobjectgoesaway,t

C++ const_cast 用法而不是 C 风格的转换

为什么会这样?constinti0=5;//inti1=const_cast(i0);//compilationerrorinti2=(int)i0;//okayinti3=5;//constinti4=const_cast(i3);//compilationerrorconstinti5=(constint)i3;//okay 最佳答案 constinti0=5;//inti1=const_cast(i0);//compilationerrorinti2=(int)i0;//okayinti3=5;//constinti4=con

c++ - cppreference 中 atomic_compare_exchange_weak 的示例代码是否正确?

在http://en.cppreference.com/w/cpp/atomic/atomic_compare_exchange,以下示例代码作为std::atomic_compare_exchange_weak的示例使用:voidappend(list*s,node*n){node*head;do{head=s->head;n->next=head;}while(!std::atomic_compare_exchange_weak(s->head,head,n));}我的理解是这个有比较*(s->head)的效果与head,当我认为需要的是比较s->head与head.第一个参数应该

c++ - 工厂、unique_ptr 和 static_cast

考虑具有基对象、派生接口(interface)和最终对象的多态类://baseobjectstructobject{virtual~object()=default;};//interfacesderivedfrombaseobjectstructinterface1:object{virtualvoidprint_hello()const=0;templatestaticvoidon_destruction(object*/*ptr*/){std::cout在实际用例中,最终对象是通过插件系统定义的,但这不是重点。请注意,我希望能够在销毁对象时调用on_destruction(请参阅

c++ - shared_ptr 需要完整的类型;不能与 lua_State 一起使用*

我正在为Lua编写C++/OOP包装器。我的代码是:classLuaState{boost::shared_ptrL;LuaState():L(luaL_newstate(),LuaState::CustomDeleter){}}问题是lua_State是不完整的类型,而shared_ptr构造函数需要完整的类型。我需要安全的指针共享。(有趣的是,boost文档说大多数函数不需要完整类型,但构造函数需要,所以没有办法使用它。http://www.boost.org/doc/libs/1_42_0/libs/smart_ptr/smart_ptr.htm)我能解决这个问题吗?谢谢。

c++ - shared_ptr - 按值传递与按引用传递

假设我有:typedefboost::shared_ptrEventPtr;在一个线程上,我正在创建一个Event并将其发送出去以进行分派(dispatch):Event*event=newEvent();EventPtreventPtr(event);EventDispatcher::dispatch(eventPtr);//pseudocodeEventDispatcher接收一个EventPtr并将其添加到一个队列中,该队列在另一个线程中进行处理...但是什么是适合调度方法的方法签名?dispatch(EventPtrevent);//willpush_back(event)或d

c++ - 这是 const_cast 未定义的行为吗?

我想知道以下是否是未定义的行为//Case1:int*p=0;intconst*q=*const_cast(&p);//Case2:(Ithinkthisisthesame)int*p=0;intconst*const*pp=&p;intconst*q=*pp;读取int*是否是未定义的行为,就好像它是intconst*一样?我认为这是未定义的行为,但我以前认为通常只添加const是安全的,所以我不确定。 最佳答案 资格方面,没问题。将每个表达式拆分为一个语句:int*p=0;//okint**addrp=&p;//okintcon

c++ - 确保抽象基类是一个 shared_ptr

我有一个抽象基类:structBase:std::enable_shared_from_this{virtual~Base()=default;virtualvoidfoo()=0;voidbar(){baz(shared_from_this());}};Base的唯一有效用例是存在于shared_ptr中-bar是一个重要的方法。我怎样才能确保以下情况是不可能的:structBadDerived:Base{voidfoo()override{...}};BadDerivedbd;bd.bar(); 最佳答案 一种技术是将Base的

c++11 - std::shared_ptr 和 dlopen(),避免未定义的行为

dlopen()是一个C函数,用于在运行时动态加载共享库。如果您不熟悉,该模式是这样的:调用dlopen("libpath",flag)得到void*handle去图书馆调用dlsym(handle,"object_name")得到void*object到图书馆想要的东西用object做你想做的事调用dlclose(handle)卸载库。在C++中,这是std::shared_ptr的所谓别名构造函数的完美用例。.模式变为:构造一个std::shared_ptrhandle来自dlopen("libpath",flag)那会调用dlclose()当它的析构函数被调用时构造一个std::

c++ - 不使用 reinterpret_cast 读取二进制数据

只是因为在我编写读取二进制STL文件的程序之前,我从未读取过二进制文件。我使用带有char*参数的ifstream读取成员。为了将我的结构转换为char*,我使用了reinterpret_cast。但据我所知,我读过的每本关于C++的书都说过类似“除非你必须使用,否则不要使用reinterpret_cast”之类的话。有什么更好的方法来读取二进制数据,不一定是直接读取,但最终读取到一个结构中并且不需要reinterpret_cast?主要功能:std::ifstreamin(cmdline[1].c_str(),std::ios::binary);in.seekg(80,std::if