草庐IT

const-cast

全部标签

c++ - "Expected ' (' for function-style cast or type construction"错误是什么意思?

我收到错误“Expected'('forfunction-stylecastortypeconstruction”,我已尽力在线研究此错误的含义,但无法找到导致此错误的任何文档错误。我在StackOverflow上发现的所有相关问题都修复了特定的代码片段,并且没有更笼统地解释导致错误的原因。这些包括Expected'('forfunction-stylecastortypeconstruction答案突出了代码的几个问题。究竟是哪个问题导致了错误尚不清楚。c++Xcodeexpected'('forfunction-stylecastortypeconstruction在主函数中定义函

c++ - 在可能的情况下,C++ 是否总是更喜欢右值引用转换运算符而不是 const 左值引用?

在编写转换运算符时,如果我同时提供到constT&和T&&的转换,C++是否会尽可能优先选择右值运算符?在这个小测试中似乎是这样:#include#includestructholds{operatorint&&(){printf("moving!\n");returnstd::move(i);}operatorconstint&()const{printf("copying!\n");returni;}private:inti=0;};intmain(){holdsh;intval=h;}打印:╰─▸./testmoving!但也许有人说的专业知识比我验证的要好?

c++ - Delphi 中 dynamic_cast 的等价物是什么?

在Delphi中,C++的dynamic_cast、reinterpret_cast和static_cast运算符(尤其是在对象上使用时)的等效项是什么? 最佳答案 重新解释_cast大多数时候,在Delphi中,转换是reinterpret_cast,即一种类型的位和字节被重新解释为另一种类型,例如Integer(myEnum)或Pointer(MyDynamicArrayVar)。有些转换会截掉位,即Integer(MyInt64)会截掉Int64的高32位,而低32位的高位会变成新的符号位。一些Actor阵容扩大,例如Inte

c++ - 为什么 delete 运算符可以用在 const 上下文中?

这个问题不同于:Isadestructorconsideredaconstfunction?new-expressionanddelete-expressiononconstreferenceandconstpointerDeletingapointertoconst(Tconst*)我写了一个这样的类Test。classTest{private:int*p;public://constructorTest(inti){p=newint(i);}Test&operator=(constTest&rhs){deletep;p=newint(*(rhs.p));return*this;}}

c++ - 为什么要经历 static_cast 的麻烦——将数字转换为 double ?

在我正在处理的代码中遇到了这个问题: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

c++ - 为什么 C++ 允许但忽略将 const 应用于函数类型?

我从探索C++的不寻常角落中得到真正的乐趣。从thisquestion了解了真正的函数类型而不是函数指针,我试着搞乱函数类型并想出了这个奇怪的案例:typedefintFunc(int);intFoo(intx){return1;}intmain(){constFunc*&f=&Foo;return0;}因为&Foo是Func*类型的右值,我想我应该可以把它放在一个const引用中,但是我从g++4.6得到这个错误:funcTypes.cpp:Infunction‘intmain()’:funcTypes.cpp:7:23:error:invalidinitializationofno

c++ - 错误 : passing const xxx as this argument of xxx discards qualifiers

我在将仿函数从Windows移植到Linux时遇到问题。(传递给STL::map以进行严格弱排序的仿函数)原文如下:structstringCompare{//Utilizedasafunctorforstl::mapparameterforstringsbooloperator()(stringlhs,stringrhs){//Returnstrueiflhs由于linux不支持_stricmp而是使用strcasecmp,我将其更改为:structstringCompare{booloperator()(stringlhs,stringrhs){//Returnstrueiflhs

c++ - 只读内存映射寄存器在 C 中定义为 `volatile const` 但在 C++ 中仅定义为 `volatile`

在使用AtmelSAM3X8E处理嵌入式系统项目时,我注意到某些CMSIS头文件中有以下代码。#ifndef__cplusplustypedefvolatileconstuint32_tRoReg;/**为什么C++的typedef不包含const?我在某处看到有人提到C++不会在运行时内存中存储整数const变量,如果为真,则意味着const需要被删除,因为微Controller寄存器是如何映射内存的,但我可以'似乎没有找到任何其他说明C++可以做到这一点的内容(尽管我的搜索确实非常简短)。没有太多的C++经验,我还认为可能是C++不允许const结构成员,因为这些typedef主要

C++ 连接字符串导致 "invalid operands of types ‘const char*’ 和 ‘const char"

我想连接两个字符串,但出现错误,我不知道如何克服这个错误。有什么方法可以将这个constchar*转换为char吗?我应该使用一些取消引用吗?../src/main.cpp:38:error:invalidoperandsoftypes‘constchar*’and‘constchar[2]’tobinary‘operator+’make:***[src/main.o]Error1但是,如果我尝试以这种方式组成“bottom”字符串,它会起作用:bottom+="|";bottom+=tmp[j];bottom+="";这是代码。#include#include#include#inc

c++ - 通过引用 const char* 传递字符串文字无法使用 g++ 4.6.3 进行编译

这是来自C++Primer,第4版,第16章的示例,它是关于模板特化的。templateintcompare(constT&v1,constT&v2){if(v1intcompare(constchar*const&v1,constchar*const&v2){returnstrcmp(v1,v2);}intmain(intargc,constchar*argv[]){cout我预计compare("abc","defg")将调用模板的专用版本。但事实是,g++4.6.3不会编译此代码并给出以下错误:error:nomatchingfunctionforcallto'compare(c