草庐IT

dynamic_casting

全部标签

c++ - 使用 reinterpret_cast 将函数强制转换为 void*,为什么不违法?

这是对我之前的问题Theaddressofafunctionmatchingaboolvsconstvoid*overload的切线跟进.回答者解释说:The[C++11]standarddoesnotdefineanystandardconversionsfroma"pointertofunction"toa"pointertovoid."It'shardtoprovideaquotefortheabsenceofsomething,buttheclosestIcandoisC++114.10/2[conv.ptr]:Aprvalueoftype“pointertocvT,”wher

c++ - reinterpret_cast moSTLy 没用吗?

我已经阅读了关于theuseofreinterpret_cast的各种先前问题。,并且我还阅读了C++标准中的相关措辞。本质上,它归结为指针到指针reinterpret_cast操作的结果不能安全地用于任何东西,除了被转换回原始指针类型。然而,在实践中,reinterpret_cast的大多数实际使用似乎是基于(错误的)假设,即reinterpret_cast与C风格相同throw。例如,我看到很多代码使用reinterpret_cast将char*转换为unsignedchar*以进行字符集转换例行公事。这是完全无害的,但严格来说它不是可移植的-不能保证从char*到unsigned

c++ - reinterpret_cast moSTLy 没用吗?

我已经阅读了关于theuseofreinterpret_cast的各种先前问题。,并且我还阅读了C++标准中的相关措辞。本质上,它归结为指针到指针reinterpret_cast操作的结果不能安全地用于任何东西,除了被转换回原始指针类型。然而,在实践中,reinterpret_cast的大多数实际使用似乎是基于(错误的)假设,即reinterpret_cast与C风格相同throw。例如,我看到很多代码使用reinterpret_cast将char*转换为unsignedchar*以进行字符集转换例行公事。这是完全无害的,但严格来说它不是可移植的-不能保证从char*到unsigned

c++ - dynamic_cast 如何失败?

根据我的阅读,执行错误的运行时dynamic_cast可能会抛出bad_cast异常或返回零。如果你正在转换指针,它会返回零是否正确?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Base*base=newBase();dynamic_cast(base);return0;}而且它会在转换对象时抛出一个bad_cast异常?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Basebase;Base&ref=base;d

c++ - dynamic_cast 如何失败?

根据我的阅读,执行错误的运行时dynamic_cast可能会抛出bad_cast异常或返回零。如果你正在转换指针,它会返回零是否正确?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Base*base=newBase();dynamic_cast(base);return0;}而且它会在转换对象时抛出一个bad_cast异常?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Basebase;Base&ref=base;d

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++ - 为什么强类型枚举可以用没有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++ - 在没有 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