草庐IT

weak_ptr_cast

全部标签

c++ - 奇怪的运算符重载, "operator T& () const noexcept { return *_ptr; }"

我研究了一下,operator函数的格式是(returnvalue)operator[space]op(arguments){implementation}但是,在std::reference_wrapper实现中,有一个运算符重载函数声明为operatorT&()constnoexcept{return*_ptr;。这个运算符和T&operator()constnoexcept{return*_ptr;不同吗?}?.如果两者不同,那么第一个有什么用? 最佳答案 运算符T&()constnoexcept;是一个user-define

c++ - 为什么我不能将 std::unique_ptr 用作 "template<class> class"参数?

这段代码:#includetemplateclassPtr>classA{Ptrints;};usingB=A;产生以下错误(使用GCC6.3):a.cpp:6:28:error:type/valuemismatchatargument1intemplateparameterlistfor‘templateclassPtr>classA’usingB=A;^a.cpp:6:28:note:expectedatemplateoftype‘templateclassPtr’,got‘templateclassstd::unique_ptr’现在,我可以像这样解决这个问题:templateu

已解决java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String异常的

已解决java.lang.ClassCastException:classjava.lang.Integercannotbecasttoclassjava.lang.String异常的正确解决方法,亲测有效!!!文章目录报错问题解决思路解决方法交流报错问题java.lang.ClassCastException:classjava.lang.Integercannotbecasttoclassjava.lang.String解决思路java.lang.ClassCastException:classjava.lang.Integercannotbecasttoclassjava.lang.St

c++ - 如何获得指向 shared_ptr 的指针?

我现在正在破解一个旧的C代码,试着让它更像C++/Boost风格:有一个资源分配函数如下所示:my_src_type*src;my_src_create(&src,ctx,topic,handle_src_event,NULL,NULL);我尝试用shared_ptr包装src:shared_ptrpSrc;刚才忘记说了。我需要循环执行此操作std::map>dict;my_src_type*raw_ptr;BOOST_FOREACH(std::stringtopic,all_topics){my_src_create(&raw_ptr,ctx,topic,handle_src_eve

c++ - 代码审查问题——我应该允许将 auto_ptr 作为参数传递吗?

考虑我最近在我们的代码库中看到的以下示例代码:voidClassA::ExportAnimation(auto_ptranimation){...doessomething}//callingmethod:voidclassB::someMethod(){auto_ptranimation(newCAnimation(1,2));ClassAclassAInstance;classAInstance.ExportAnimation(animation)...dosomemorestuff}我不喜欢这样——我宁愿这样写:voidClassA::ExportAnimation(CAnima

c++ - 如何使用 reinterpret_cast 转换为 C++ 中的派生类指针

这是我的测试示例:structbase{virtual~base(){}intx;};structderived:publicvirtualbase{base*clone(){returnnewderived;}derived():s("a"){}std::strings;};intmain(){derivedd;base*b=d.clone();derived*t=reinterpret_cast(b);std::couts它在我打印s的那一行崩溃了。由于“b”是指向派生类的指针,因此reinterpret_cast应该可以正常工作。我想知道为什么它会崩溃。同时,如果我用dynami

c++ - 无法将参数 1 从 'cli::interior_ptr<Type>' 转换为 'CvCapture **'

我正在抓取一个视频帧如下CvCapture*capture=cvCreateFileCapture("PATH");我可以阅读视频并处理它。一切正常。但是当我尝试释放捕获时cvReleaseCapture(&capture);我明白了errorC2664:'cvReleaseCapture':cannotconvertparameter1from'cli::interior_ptr'to'CvCapture**'with[Type=CvCapture*]Cannotconvertamanagedtypetoanunmanagedtype函数在一个类中。publicrefclassLoc

c++ - 使用 std::shared_ptr 对象实例创建 boost::thread

我有以下两个代码段。第一个block按预期编译和工作。但是第二个block不编译。我的问题是,给定下面的代码,当尝试基于由shared_ptr代理的对象实例创建线程时,正确的语法是什么?#include#include#include#includestructfoo{voidboo(){}};intmain(){//Thisworks{foo*fptr=newfoo;boost::threadt(&foo::boo,fptr);t.join();deletefptr;}//Thisdoesn'twork{std::shared_ptrfptr(newfoo);boost::threa

c++ - 是否可以使用 dynamic_cast 进行模板类型检查?

templatevoidcheckObject(TgenericObject){MyClassA*a=dynamic_cast(genericObject);if(a!=NULL){//weknowitisoftypeMyClassA}MyClassB*b=dynamic_cast(genericObject);if(b!=NULL){//weknowitisoftypeMyClassB}}这样的事情可能吗?我们有一个模板类型,但我们想知道它是实际类型吗? 最佳答案 在模板世界中,您可能只想为每种类型专门化模板,而不是进行运行时检查

c++ - 将深拷贝构造函数添加到 std::unique_ptr<my_type>

我想存储一些std::unique_ptr进入std::vector.自my_type提供一个clone()制作my_type*的深拷贝非常简单.重点是如何扩展std::unique_ptr在添加复制构造函数和赋值运算符的同时保留其所有功能。遗产?模板特化?你能提供一个代码片段吗? 最佳答案 std::unique_ptr的目的是使其唯一,即它不应该是可复制的。这就是为什么他们将其设为只能移动的原因。它用于表示唯一所有权。如果你想做一个深拷贝然后让你的拷贝构造函数完成它的工作,这就是它的用途。std::unique_ptrptr1{