草庐IT

Implicit_cast

全部标签

c++ - 在处理低级字节操作时 reinterpret_cast 不好吗?

我正在编写一个websocket服务器,我必须处理需要取消屏蔽的屏蔽数据。掩码是unsignedchar[4],数据也是unsignedchar*buffer。我不想逐字节异或,我更愿意一次异或4个字节。uint32_t*constend=reinterpret_cast(data_+length);for(uint32_t*i=reinterpret_cast(data_);i!=end;++i){*i^=mask_;}在这种情况下使用reinterpret_cast有什么问题吗?替代方案是以下代码,它不那么清晰且速度不快:uint64_tj=0;uint8_t*end=data_+

c++ - floor 和 duration_cast 有什么区别?

所以在c++11Chrono图书馆提供,duration_cast:Computationsaredoneinthewidesttypeavailableandconverted,asifbystatic_cast,totheresulttypeonlywhenfinished和c++17的floor:ReturnsthegreatestdurationtrepresentableinToDurationthatislessorequaltod所以对于所有x这两个调用的结果是否相等:chrono::duration_cast(x)chrono::floor(x)

c++ - static_cast 的指针值

在当前标准草案(和C++17)中,this是关于static_castingvoid*的:Aprvalueoftype“pointertocv1void”canbeconvertedtoaprvalueoftype“pointertocv2T”,whereTisanobjecttypeandcv2isthesamecv-qualificationas,orgreatercv-qualificationthan,cv1.IftheoriginalpointervaluerepresentstheaddressAofabyteinmemoryandAdoesnotsatisfytheali

c++ - 地址、reinterpret_cast 和多重继承

任何人都可以解释以下代码的行为吗?为什么在第一种情况下我们有b=3,即b2==&d为真?为什么在案例2中没问题?b2和d的地址我打印出来了,它们是不一样的。#includeusingnamespacestd;classA{public:A():m_i(0){}protected:intm_i;};classB{public:B():m_d(0.0){}protected:doublem_d;};classC:publicA,publicB{public:C():m_c('a'){}private:charm_c;};intmain(){Cd;B*b2=&d;cout(b2)==rein

c++ - 关于c++中const_cast的问题

全部:这是引用自EffectiveC++第三版const_castistypicallyusedtocastawaytheconstnessofobjects.ItistheonlyC++-stylecastthatcandothis.我的问题是const_cast可以为非const对象添加常量吗?其实我写了一个小程序来验证我的想法。classConstTest{public:voidtest(){printf("callingnon-constversiontestconstfunction\n");}voidtest()const{printf("callingconstversi

c++ - reinterpret_cast 与 static_cast 用于在标准布局类型中写入字节?

我需要写入某些整数类型的单个字节。我应该使用reinterpret_cast,还是应该通过void*使用static_cast?(一)unsignedshortv16;char*p=static_cast(static_cast(&v16));p[1]=...somecharvaluep[0]=...somecharvalue或(b)unsignedshortv16;char*p=reinterpret_cast(&v16);p[1]=...somecharvaluep[0]=...somecharvalue根据static_castandreinterpret_castforstd:

c++ - 为什么在这里使用 static_cast 而不是 reinterpret_cast 很重要?

AtareplyofablogpostofRaymondChen,提问者指出Raymond,IbelievetheC++exampleisnotcorrectsincethepositionofthebaseclasssubobjectinthederivedclassisunspecifiedaccordingtoISOC++2003Standard(10-3,page168),andyouassumethatthebaseclasssubobjectisalwaysatthebeginning.TheCexamplewouldbefineinC++too,soI'dstickwit

c++ - dynamic_cast<>有多快

...大约与典型的std::string::operator==()相比?我在下面提供了更多详细信息,我不确定它们是否有任何相关性。具有复杂性或近似值的答案就足够了。谢谢!详细信息:我将在列表的for循环中使用它来查找一些特定实例。我估计我的平均继承水平为3.5类。我正在寻找的那个有一个父类、一个祖父类和在这两个“接口(interface)”之上,即用几个virtualvoidabc()=0;来抽象类。没有我要查找的子类。 最佳答案 这在很大程度上取决于您的编译器、特定的类层次结构、硬件以及各种因素。您确实需要直接在您的特定应用程序

c++ - 如果与指针一起使用,dynamic_cast 何时会抛出异常?

我在源代码中使用dynamic_cast将指针转换为如下所示,Base*base=herestoringthepointer;Derived*derived=dynamic_cast(base);如果base没有类层次结构的指针,则转换失败并返回NULL。在接下来的几行中,我正在检查NULL。所以没问题。我遇到了故障转储,我的应用程序由于dynamic_cast抛出异常而崩溃。我知道dynamic_cast只有在与引用类型一起使用时才会抛出异常。知道当我在上面的源代码中使用指针时,dynamic_cast会抛出异常吗? 最佳答案 A

c++ - 使用 dynamic_cast 转换 const 类

我想投这个:classBase{public:virtual~Base(){};};classDer:publicBase{};intmain(){constBase*base=newDer;Der*der=dynamic_cast(base);//Errorreturn0;}我该怎么办?我试着输入:constDer*der=dynamic_cast(base);维护常量,但这不起作用。 最佳答案 试试这个:constDer*der=dynamic_cast(base);dynamic_cast无法删除const限定符。您可以使用c