草庐IT

LONG_PTR

全部标签

c++ - 为什么复制 const shared_ptr& 不会违反 const-ness?

尽管我的代码编译得很好,但这一直困扰着我,我无法在stackoverflow上找到答案。以下通用构造函数是将shared_ptr传递给构造函数中的类实例的一种方法。MyClass{MyClass(conststd::shared_ptr&pt);std::shared_ptrpt_;//EDITED:Removed&typo};MyClass::MyClass(conststd::shared_ptr&pt):pt_(pt){}这编译得很好。我的问题如下:在我的理解中,像这样声明一个参数const:voidmyfunc(constT&t)promise不改变t。但是,通过将shared

c++ - 为什么让 operator new 私有(private)中断 std::shared_ptr?

我正在实现一个多态类型(称之为A),我想通过std::shared_ptr专门管理它。为了允许在派生类的构造函数中使用shared_from_this,A使用new分配,然后初始化一个成员std::shared_ptr给自己自动管理它的生命周期。为了帮助实现这一点,我决定将类特定的operatornew设为私有(private),并计划使用create()辅助函数而不是new和make_shared。该设计可能看起来有点滑稽,但在我正在处理的UI库的上下文中是有意义的。一个最小的可重现示例如下:structA{A():m_sharedthis(this){}voiddestroy(){

c++ - 尝试从 vector 中获取 unique_ptr 元素后出错

我阅读了有关如何插入unique_ptr的信息进入vector>:WhycanInotpush_backaunique_ptrintoavector?但是我如何取回一个元素呢?我举了个例子:#include#includeclassA{public:A(intx,inty){x_=x;y_=y;}private:intx_;inty_;};intmain(){std::vector>vec_a;std::unique_ptrtmp_a=std::unique_ptr(newA(13,32));vec_a.push_back(std::move(tmp_a));vec_a.push_ba

c++ - vector<auto_ptr<>> 的编译问题

考虑以下代码:#include#include#includeusingnamespacestd;structA{inta;A(inta_):a(a_){}};intmain(){vector>as;for(inti=0;ia(newA(i));as.push_back(a);}for(vector>::iteratorit=as.begin();it!=as.end();++it)couta当尝试编译它时,我从g++得到以下模糊的编译器错误:g++-O0-g3-Wall-c-fmessage-length=0-MMD-MP-MF"src/proba.d"-MT"src/proba.d

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++ - c++中 "unsigned long int"的最大值

我如何知道“unsignedlongint”类型的变量的最大可赋值是多少? 最佳答案 显而易见的方法是使用std::numeric_limits::max(); 关于c++-c++中"unsignedlongint"的最大值,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18301685/

c++ - 为什么 "long *"和 "int *"在 32 位代码中不兼容?

我想知道为什么下面的代码不能编译:voidfoo_int(int*a){}voidfoo_long(long*a){}intmain(){inti;longl;foo_long(&i);foo_int(&l);}我使用的是GCC,在C或C++中都无法调用。由于是32位系统,int和long都是有符号的32位整数(可以在编译时用sizeof验证)。我问的原因是我有两个单独的头文件,它们都不在我的控制之下,一个做类似的事情:typedefunsignedlongu32;另一个:typedefunsignedintuint32_t;。声明基本上是兼容的,除了当我像上面的代码片段那样将它们用作

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