草庐IT

const_cast-ing

全部标签

c++ - 为什么我们使用 static_cast 为 NULL

当我尝试研究QP/CPP代码时,我遇到了以下行。QTimeEvt*t;//...if(t==static_cast(0)){为什么他们要做0的static_cast?如果他们想检查NULL,我们可以直接这样做吗?这个源代码你可以在中找到http://www.state-machine.com/qpcpp/qf__time_8cpp_source.html 最佳答案 是的,这是不必要的,尽管它可能是某些风格指南为了“清晰”而强制要求的,或者它可能是为了让过度热心的静态分析工具沉默。当然,如今,我们只需编写nullptr就可以了。

c++ - C++ 中的 const_cast 规则

structfoo{constintA;intB;foo():A(10),B(20){}};voidmain(){foof1;const_cast(f1.A)=4;//line1constfoof2;const_cast(f2.B)=4;//line2}第1行和第2行是否都表现出未定义的行为?如果f1和f2是上面代码中列出的类型的shared_ptr,行为会有所不同吗? 最佳答案 两者的行为const_cast(f1.A)=4和const_cast(f2.B)=4未定义。如果一个对象最初定义为const,然后你扔掉了const-ne

c++ - 从 ‘const char*’ 到 ‘char*’ [-fpermissive] 的无效转换

int::cadenacalculatelenght(constcadena&a,constchar*cad){cadenac;intlenght=0;char*punt;punt=cad;while(*punt){lenght++;punt++;}returnlenght;}我有这个问题,我想计算C字符串的长度而不使用像strlen这样的函数,在我的cadena类的其他方法中我可以,因为它不是constchar*,但现在我不知道该怎么办。 最佳答案 您可以将punt声明为正确的类型:constchar*punt=cad;

c++ - 将 double 分配给 const int& vs int 分配给 const int&

这个问题在这里已经有了答案:TemporaryinitializationandReferenceinitialization(3个答案)关闭4个月前。我试图理解C++中的常量引用,但我偶然发现了这个问题:当我将double分配给constint&然后更改引用double的值时,我的int引用的值保持不变。doublei=10;constint&ref=i;i=20;cout而在分配int时,值会发生变化。inti=10;constint&ref=i;i=20;cout这种行为的原因是什么?我的猜测是,在分配double时,隐式转换为int创建了一个新对象,然后分配了它的引用,但是我找

c++ - 为什么 decltype 从内置类型的返回类型中删除 const?

作为一般规则,decltype保留常量:constintci=0;decltype(ci)x;//xisconstintx=5;//error--xisconstclassGadget{}:constGadgetmakeCG();//factorydecltype(makeCG())y1,y2;//y1andy2areconstGadgetsy1=y2;//error--y1isconst但对于返回基本类型的const返回类型,decltype似乎抛弃了const:constintmakeCI();//factorydecltype(makeCI())z;//zisNOTconstz=

c++ - 为什么不能实例化带有 "non const"复制构造函数的对,而没有实例化一对是可能的?

假设您有以下类(class):structA{A(){}A(A&)=delete;};intmain(){std::pairp1;return0;}以下代码将无法编译(使用-std=c++11和g++)并出现以下错误:/usr/include/c++/5/bits/stl_pair.h:Ininstantiationof‘structstd::pair’:test.cpp:13:23:requiredfromhere/usr/include/c++/5/bits/stl_pair.h:127:17:error:‘constexprstd::pair::pair(conststd::pa

C++ 标准 : Unexpected const_iterator in multiset

我最近遇到了一个奇怪的问题,在遍历多重集时,我得到的是const_iterator而不是预期的iterator。结果证明这对MSVC来说不是问题,但g++给了我一个错误:error:invalidinitializationofreferenceoftype'myPtr&'fromexpressionoftype'constboost::shared_ptr'相关代码:typedefstd::multisetmyList;myList_mystuff;voidtick(floatdt){for(myList::iteratori=_mystuff.begin();i!=_mystuff

c++ - 两个版本的函数可以重载,常量成员函数和不带 const 的函数

我刚遇到各种重载方法,如传递的参数类型、不同数量的参数、返回类型等。我只想知道我可以用以下两个版本重载函数//functionwhichcanmodifymemberString&MyClass::doSomething();//constantmemberfunctionString&MyClass::doSomething()const;请告诉我背后的原因。 最佳答案 是的,你可以。如果你有MyClassm;m.doSomething();将调用非常量版本。当你有constMyClassm;m.doSomething();将调用

c++ - C++11 中 map 标准的 at() const 访问器是什么?

我试图找出如何在const方法中从map返回一个值,我偶然发现了gcc4.6中map的at()方法。当我查看它时,我意识到它是非标准的:C++mapaccessdiscardsqualifiers(const)但它确实比find()方法要简洁得多。我想知道C++11是否已纠正此问题-at()formap是新标准的一部分吗? 最佳答案 是的。std::map在C++11中有一个at成员函数,其规范如下(23.4.4.3/9):T&at(constkey_type&x);constT&at(constkey_type&x)const;R

C++,从 ‘char’ 到 ‘const char*’ 的无效转换

在C++中我有这个程序:#includeusingnamespacestd;intmain(){stringexpression_input;cout我收到以下错误:prog.cpp:15:error:invalidconversionfrom‘char’to‘constchar*’prog.cpp:15:error:initializingargument2of‘std::basic_string&std::basic_string::insert(typename_Alloc::rebind::other::size_type,const_CharT*)[with_CharT=ch