草庐IT

production_cast

全部标签

c++ - 将 C 风格的转换更改为 static_cast 总是安全的吗?

由于cppcheckcstyleCast样式警告,我正在尝试消除代码库中的所有C样式转换。将C风格的转换更改为static_cast总是安全的吗?安全,我的意思是,是否存在旧的C风格转换可以正常工作,但static_cast会引发错误或未定义行为的情况?type1a;type2b=(type2)a;//Cstylecasttype2b=static_cast(a);//Isthisalwaysavalidreplacementforabovecast? 最佳答案 C风格的转换通常是static_cast的组合或reinterpret

c++ - dynamic_cast<> 将变量参数传递给模板

我有一个执行测试用例的C++应用程序。某些测试用例可能会依赖于其他测试用例的输出。所有测试用例都实现一个基本接口(interface):///baseclassforalltestcasesclassITest{public:virtualvoidExecute()=0;};产生一些可能对其他测试用例有用的对象的测试用例实现这个接口(interface):///implementedbytestcasesthatprovidedatatoothertestcasestemplateclassIDependency{public:virtualObjGet()=0;};需要来自其他测试用

c++ - reinterpret_cast - 奇怪的行为

我遇到了与reinterpret_cast相关的奇怪错误。看看下面的代码:int*var;reinterpret_cast(&var);VSC++2010错误:errorC2440:'reinterpret_cast':cannotconvertfrom'int**'to'constvoid**'gcc4.1.2中的错误:从类型“int**”到类型“constvoid**”的reinterpret_cast抛弃了常量gcc4.6.2中的错误:从类型“int**”到类型“constvoid**”的reinterpret_cast丢弃限定符有没有人知道为什么编译器说我要放弃const。我和

c++ - reinterpret_cast 本身会导致异常吗?

假设我有一个名为A的类和一个空指针vp。以下是否会导致异常?A*ap=reinterpret_cast(vp);谢谢,飞悦 最佳答案 不,都不是reinterpret_cast其C风格的转换等价物也不会执行任何检查,因此它们本身不会导致异常。显然,由于这两种构造都尽可能不安全,因此取消引用结果指针ap可能导致未定义的行为。 关于c++-reinterpret_cast本身会导致异常吗?,我们在StackOverflow上找到一个类似的问题: https://s

c++ - static_cast wchar_t* 到 int* 或 short* - 为什么它是非法的?

在MicrosoftVC2005和g++编译器中,以下会导致错误:在win32VC2005上:sizeof(wchar_t)为2wchar_t*foo=0;static_cast(foo);结果errorC2440:'static_cast':cannotconvertfrom'wchar_t*'to'unsignedshort*'...在MacOSX或Linuxg++上:sizeof(wchar_t)为4wchar_t*foo=0;static_cast(foo);结果error:invalidstatic_castfromtype'wchar_t*'totype'unsignedi

c++ - "cast"指向 C++ 中函数指针的成员函数指针的最简单方法是什么?

我想为STL算法的“comp”参数提供一个成员函数,例如lower_bound(...,Comparecomp)。comp()函数访问非静态成员字段,因此它本身必须是非静态成员,但非静态成员函数指针的类型与普通函数指针的类型不同。解决这个问题的最佳方法是什么? 最佳答案 这是std::mem_fun和std::mem_fun_ref最常见的用法。它们是创建调用指定成员函数的仿函数的模板。TR1添加了一个std::tr1::bind,它也很有用且用途更广(如果您没有可用的TR1,那是基于Boost::bind的)。C++0x将在标准库

C++ strict-aliasing agnostic cast

我在StackOverflow中阅读了很多关于严格别名的QA,但它们都很常见,而且讨论总是倾向于引用C++标准的深层细节,这些细节几乎总是难以正确理解。特别是当标准不直接说,而是用一种含糊不清的方式描述某些东西时。所以,我的问题可能与这里的大量QA重复,但是请只回答一个具体问题:做一个“nonalias_cast”是正确的方法吗?:templateinlineautononalias_cast(IN*data){char*tmp=reinterpret_cast(data);returnreinterpret_cast(tmp);}floatf=3.14;unsigned*u=nona

c++ - "dynamic_cast"之后的 NULL 指针实际上可以取消引用吗?

以下代码编译正确并得到神秘的输出:specialInvestmentfunction00000000(环境:C++VS2010)#include#includeusingnamespacestd;classSecurity{public:virtual~Security(){}};classStock:publicSecurity{};classInvestment:publicSecurity{public:voidspecial(){cout(p)->special();cout(p)怎么可能呢?取消引用NULL指针并获得“正确”输出而不是崩溃?是VS2010的特殊“特性”吗?现在

javascript - 将 JS 数字数组传递给 emscripten C++ 而无需 reinterpret_cast

我在JS中有大量数组,我想将其传递给C++进行处理。恕我直言,最有效的方法是让JS直接写入C++堆并在直接调用中将指针作为参数传递,例如:varsize=4096,BPE=Float64Array.BYTES_PER_ELEMENT,buf=Module._malloc(size*BPE),numbers=Module.HEAPF64.subarray(buf/BPE,buf/BPE+size),i;//Populatethearrayandprocessthenumbers:parseResult(result,numbers);Module.myFunc(buf,size);处理数

c++ - 通过继承层次结构进行 dynamic_casting 是不好的做法吗?

我有以下数据结构:classElement{std::stringgetType();std::stringgetId();virtualstd::vectorgetChildren();}classA:publicElement{voidaddA(constA*a);voidaddB(constB*b);voidaddC(constC*c);std::vectorgetChildren();}classB:publicElement{voidaddB(constB*b);voidaddC(constC*c);std::vectorgetChildren();}classC:publi