在Delphi中,C++的dynamic_cast、reinterpret_cast和static_cast运算符(尤其是在对象上使用时)的等效项是什么? 最佳答案 重新解释_cast大多数时候,在Delphi中,转换是reinterpret_cast,即一种类型的位和字节被重新解释为另一种类型,例如Integer(myEnum)或Pointer(MyDynamicArrayVar)。有些转换会截掉位,即Integer(MyInt64)会截掉Int64的高32位,而低32位的高位会变成新的符号位。一些Actor阵容扩大,例如Inte
在我正在处理的代码中遇到了这个问题:doublepart2=static_cast(2)*somthing1*(static_cast(1)+something2)+(static_cast(1)/static_cast(2))*something3+(static_cast(1)/static_cast(2))*pow(something4,3);(something是doubles。)我怀疑有一个很好的理由来解决这个问题static_cast(1)之类的,但似乎我可以少打很多字。我不明白什么?提前致谢。 最佳答案 许多stati
classGAGenome{virtualvoidmethod(){};};templateclassGAArray{};templateclassGA1DArrayGenome:publicGAArray,publicGAGenome{};intmain(){GA1DArrayGenomegenome;constGAGenome&reference=genome;autocast=dynamic_cast&>(reference);}这个明显错误的程序(因为模板参数不同)崩溃了terminatecalledafterthrowinganinstanceof'std::bad_cast
我读过reinterpret_cast如果使用不当可能会很危险。所以我相信我使用得当;)。我发现如果我有模板类并且需要类型转换,那么使用它会很好。但最近我读到reinterpret_cast也是不可携带的。我为这一点感到难过。什么原因?拿下面的代码,voidDisp(int*val){for(inti=0;i(arr);for(unsignedchar*i=ptr;i现在输出:117421487232767419678905683925845841967200000000000000Machinetype:Linux2.6.32-358.11.1.el6.x86_64#1x86_64x
下面的程序是否违反了严格的别名规则?#includeintmain(){doubled=0.1;//std::int64_tn=*reinterpret_cast(&d);//aliasingviolation//auton{*reinterpret_cast(&d)};//aliasingviolationautonptr{reinterpret_cast(&d)};auto&n{*nptr};++n;}VS2015没有发出警告,clang或gcc. 最佳答案 Doesthefollowingprogramviolatethest
在C++WithoutFear:ABeginner'sGuideThatMakesYouFeelSmart一书中,在第(8)章中,提到了以下关于reinterpret_cast....从一种指针类型(int)转换为另一种(char*)。因为转换改变了指向数据的解释方式,所以它被称为reinterpret_cast,而不是static_cast。*你能在这里描述一下这一段吗?特别是操作命名方式的原因?谢谢。 最佳答案 基本上,reinterpret_cast将特定位置的位模式重新解释为不同的类型。参见此处的示例:http://publ
我有以下类(class)(精简后只包含相关部分):#includeclassText{private:std::string_text;public:Text(std::string&&text):_text(std::move(text)){}operatorconststd::string&()const{return_text;}};我的问题是:如果我想获得一个conststd::string&,我可以这样做而不会受到任何惩罚吗:Texttext("fred");auto&s=static_cast(text);或者这会构造一个我最终得到引用的中间std::string吗?这种情
我需要为回调函数编写代码(它将在ATL中调用,但这并不重要):HRESULTcallback(void*myObjectVoid){if(myObjectVoid==0){returnE_POINTER;}CMyClass*myObject=static_cast(myObjectVoid);returnmyObject->CallMethod();}这里的void*保证是指向CMyClass的指针,所以static_cast是合法的。我关心的是代码必须尽可能可移植(至少对于较新版本的VisualC++)。所以super偏执狂我也倾向于检查CMyClass*指针-我的意思是如果结果为空
voidfoo(MyClass*myClass){BaseClass*pBaseClass=dynamic_cast(myClass);deletemyClass;//一般来说,dynamic_cast是如何工作的?(它像复制构造函数一样工作吗?) 最佳答案 不,那是不安全的。dynamic_cast只是一种类型转换——原始指针和转换后的指针都指向同一个对象。转换后的指针可能会指向一个稍微不同的地址(如果涉及多重继承),但它仍然指向(in)同一个对象-不会发生对象复制。编辑:我的意思是“不安全”,意思是“在您删除myClass之后,
这个问题与这个问题非常相似Whycan'tIdynamic_cast"sideways"duringmultipleinheritence?,除了强制转换确实有效-只是不在构造函数中。标题:classA{public:virtual~A(){}voidprintA();};classB{public:B();virtual~B(){}voidprintB();private:std::stringmessage_;};classC:publicA,publicB{public:C(){}virtual~C(){}};来源:voidA::printA(){cout(this);if(a)