草庐IT

dynamic_ptr_cast

全部标签

c++ - 如何将 "cast"一个双参数函数转换为一个参数函数?

在matlab中,可以这样写:S=@(x,y)x^2+y^2-1G=@(x)S(x,1);如果我有一个需要单参数函数的函数,我可以执行上述操作。我如何在c/c++中执行此操作?我有一个库函数(来自CGAL库),它期望一个本身只有一个参数的函数作为参数。理想情况下,我有一个类(SphericalHarmonics),我希望有一个接受一个参数的成员函数。所以我有:FTSphericalHarmonics::distFunction(Point_3p)(注意FT是一个类似于double的类型)但是当然当我尝试SphericalHarmonics*sh=newSphericalHarmonic

c++ - 在赋值运算符中分配 std::shared_ptr

我正在创建自己的自定义Filter类以用于boost::filtered_graph。WeightMap概念必须具有默认构造函数、复制构造函数和赋值运算符。我创建了下面的类,它有一个std::shared_ptr私有(private)成员。我的问题是我应该如何编写赋值运算符。复制构造函数没有问题,但赋值运算符不起作用。classBFDMFilter{private:constBGraph*m_battlemap;conststd::shared_ptrm_mv_ab;public:BFDMFilter():m_battlemap(nullptr),m_mv_ab(){}BFDMFilt

c++ chrono duration_cast 到毫秒结果以秒为单位

我想要自纪元以来的毫秒数。一个流行的解决方案如下所示(这里提出的这个问题的解决方案之一Gettimesinceepochinmilliseconds,preferablyusingC++11chrono)#include#includeintmain(){automillitime=std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count();std::cout通过调用g++来编译它,比如g++-std=c++11main.cpp-otimetest产生输出13726860

c++ - std::unique_ptr 试图引用已删除的函数

我已经使用Unity一段时间了,然后回来使用VisualStudio2015做一些C++。我遇到了这个类定义classA{public:A();virtual~A();A(constA&)=delete;A&operator=(constA&)=delete;private:…}这个类是动态分配的,如下所示:ObjPtrobj=ObjPtr(newA());哪里ObjPtr是定义的类型,看起来像:typedefstd::unique_ptrobjPtr;并将这些创建的对象添加到std::vector使用std::move.有一次,我需要遍历对象列表,如果我找到满足我条件的对象,请保留一

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