草庐IT

dynamic_pointer_cast

全部标签

c++ - 为什么强类型枚举可以用没有static_cast的整数初始化?

enumclassE{};intmain(){Ee1{0};//okEe2=0;//notok//error:cannotinitializeavariableof//type'E'withanrvalueoftype'int'}我的编译器是clang4.0,带有选项-std=c++1z。预计Ee2=0;不行,因为E是强类型的。不过,让我吃惊的是Ee1{0};应该没问题。为什么没有static_cast可以用整数初始化强类型枚举? 最佳答案 看reference自C++17起允许使用列表初始化器:Bothscopedenumerat

c++ - 常量不匹配 : 2 overloads have no legal conversion for 'this' pointer

我收到了这个奇怪的错误:errorC2663:'sf::Drawable::SetPosition':2overloadshavenolegalconversionfor'this'pointer我认为这与const不匹配有关,但我不知道在哪里,或者为什么。在下面的代码中,我有一个形状和Sprite的vector,当尝试访问其中一个vector形状并调用它的一个函数时,我遇到了错误。std::vectorShapes;std::vectorSprites;boolAddShape(sf::Shape&S){Shapes.push_back(S);returntrue;};boolAdd

c++ - 常量不匹配 : 2 overloads have no legal conversion for 'this' pointer

我收到了这个奇怪的错误:errorC2663:'sf::Drawable::SetPosition':2overloadshavenolegalconversionfor'this'pointer我认为这与const不匹配有关,但我不知道在哪里,或者为什么。在下面的代码中,我有一个形状和Sprite的vector,当尝试访问其中一个vector形状并调用它的一个函数时,我遇到了错误。std::vectorShapes;std::vectorSprites;boolAddShape(sf::Shape&S){Shapes.push_back(S);returntrue;};boolAdd

c++ - 如何将 "pointer to pointer type"转换为 const?

用下面的代码voidTestF(constdouble**testv){;}voidcallTest(){double**test;TestF(test);}我明白了:'TestF':cannotconvertparameter1from'double**'to'constdouble**'我不明白为什么。为什么test不能被无声地转换为constdouble**?我为什么要明确地这样做?我知道TestF(const_cast(test))使我的代码正确,但我觉得这应该是不必要的。我缺少一些关于const的关键概念吗? 最佳答案 该

c++ - 如何将 "pointer to pointer type"转换为 const?

用下面的代码voidTestF(constdouble**testv){;}voidcallTest(){double**test;TestF(test);}我明白了:'TestF':cannotconvertparameter1from'double**'to'constdouble**'我不明白为什么。为什么test不能被无声地转换为constdouble**?我为什么要明确地这样做?我知道TestF(const_cast(test))使我的代码正确,但我觉得这应该是不必要的。我缺少一些关于const的关键概念吗? 最佳答案 该

c++ - 在没有 reinterpret_cast 的情况下将 unsigned char 转换为 std::string 的方法?

我在std::string中有一个我需要的unsignedchar数组,但我目前的方式使用我想避免的reinterpret_cast。有没有更清洁的方法来做到这一点?unsignedcharmy_txt[]={0x52,0x5f,0x73,0x68,0x7e,0x29,0x33,0x74,0x74,0x73,0x72,0x55}unsignedintmy_txt_len=12;std::stringmy_std_string(reinterpret_cast(my_txt),my_txt_len); 最佳答案 使用迭代器构造函数:s

c++ - 在没有 reinterpret_cast 的情况下将 unsigned char 转换为 std::string 的方法?

我在std::string中有一个我需要的unsignedchar数组,但我目前的方式使用我想避免的reinterpret_cast。有没有更清洁的方法来做到这一点?unsignedcharmy_txt[]={0x52,0x5f,0x73,0x68,0x7e,0x29,0x33,0x74,0x74,0x73,0x72,0x55}unsignedintmy_txt_len=12;std::stringmy_std_string(reinterpret_cast(my_txt),my_txt_len); 最佳答案 使用迭代器构造函数:s

c++ - 常见问题解答 : Why does dynamic_cast only work if a class has at least 1 virtual method?

这在C++中无法编译:classA{};classB:publicA{};...A*a=newB();B*b=dynamic_cast(a); 最佳答案 因为dynamic_cast只能向下转换多态类型,所以标准这么说。您可以通过向基类添加virtual析构函数来使您的类具有多态性。事实上,你可能应该无论如何(见脚注)。否则,如果您尝试通过A指针删除B对象,您将调用UndefinedBehavior.classA{public:virtual~A(){};};瞧!脚注关于在多态类型中需要虚拟析构函数的“规则”有一些异常(except

c++ - 常见问题解答 : Why does dynamic_cast only work if a class has at least 1 virtual method?

这在C++中无法编译:classA{};classB:publicA{};...A*a=newB();B*b=dynamic_cast(a); 最佳答案 因为dynamic_cast只能向下转换多态类型,所以标准这么说。您可以通过向基类添加virtual析构函数来使您的类具有多态性。事实上,你可能应该无论如何(见脚注)。否则,如果您尝试通过A指针删除B对象,您将调用UndefinedBehavior.classA{public:virtual~A(){};};瞧!脚注关于在多态类型中需要虚拟析构函数的“规则”有一些异常(except

C++ union 与 reinterpret_cast

它出现在otherStackOverflowquestions并阅读ISO/IECdraftC++standard的§9.5.1使用union做文字的标准reinterpret_cast的数据是未定义的行为。考虑下面的代码。目标是取0xffff的整数值并将其解释为IEEE754浮点中的一系列位。(Binaryconvertshowsvisuallyhowthisisdone.)#includeusingnamespacestd;unionunionType{intmyInt;floatmyFloat;};intmain(){inti=0xffff;unionTypeu;u.myInt=