草庐IT

Const-Qualification

全部标签

c++ - 需要帮助调试从 const char* 到 char* [-fpermissive] 的无效转换

我是c++的新手,不知道为什么会这样......第105行我收到此错误从constchar*到char*的无效转换[-fpermissive]第113行我收到从âconstchar*到char*[-fpermissive]的无效转换错误#include#include#includeusingnamespacestd;characWordWrap[1024];characPrint[1024];BasicConsole::BasicConsole(char*szName):ZFSubSystem(szName){m_iMaxWidth=50;//TEXT_MAX_LENGHT;m_bL

c++ - 在自定义 const native C++ 容器类上支持 "for each"

我想实现一个简单的nativeC++固定容量数组模板类,为了方便起见支持基于范围的“foreach”语法,开销最小。我在const实例上支持它时遇到问题。有了这个实现:templateclassList{public:List(){mSize=0;}constT*begin()const{returnmItems;}constT*end()const{returnmItems+mSize;}T*begin(){returnmItems;}T*end(){returnmItems+mSize;}private:size_tmSize;TmItems[Capacity];};和这种用法:c

c++ - Qt错误: 'const class QString' has no member named 'toStdString'

我收到此错误error:'constclassQString'hasnomembernamed'toStdString'虽然QString有它。(link).代码std::stringMessage::toStdString()const{returnm_string.toStdString();} 最佳答案 直接从这里复制答案:HowtoconvertQStringtostd::string?QStringqs;//EitherthisifyouuseUTF-8anywherestd::stringutf8_text=qs.toU

c++ - 通过 const 引用延长临时生命周期

C++我正在尝试了解const引用如何延长临时对象的生命周期。我正在运行oneoftheanswerstoWhatarethedifferencesbetweenpointervariableandreferencevariableinC++?中的代码片段并在VC11和g++4.8之间得到了冲突的结果。我在这里扩展了代码段:#includestructscope_test{~scope_test(){printf("scope_testdone!\n");}};intmain(){constscope_test&test=scope_test();printf("inscope\n")

c++ - 错误 C2662 无法从 const 转换为引用

这是我关于堆栈溢出的第一篇文章,我希望将来能加入社区。我正在为ADT类编写哈希表实现;我的大部分方法都在作业范围内达到了标准,但这让我很伤心。在这个我一直用来测试我编写的各种函数的测试应用程序中,我收到错误“errorC2662:'customer::getPhone':cannotconvert'this'ponterfrom'constcustomer'to'customer&'引用行“光标=find_ptr(entry.getPhone());”和“list_head_insert(data[hash(entry.getPhone())],entry);”我的函数实现代码如下:t

c++ - 带有仿函数修改对象的 const 函数

如果我们考虑以下方法,我的印象是bar不能修改this(即Foo的实例)。structFoo{inti;//varshallnotmodifytherespectiveinstanceofFoo,thus"const"voidbar(std::functionfunc)const{func(3);}};但是,以下是可能的:voidanothermethod(){Foof;f.bar([&](intx){f.i=3;});//modifyFoo.i"within"Foo::barconst.Dangerous?}我看到方法bar不是“直接”修改其实例的值i,而是通过给定参数“间接”修改函

c++ - 当 T 是引用类型时,为什么 const T& 参数中的 const 消失了?

这个问题在这里已经有了答案:Referencecollapsing?(2个答案)关闭7年前。下面的代码表明,如果采用引用类型const参数的模板是用引用类型(例如,int&)实例化的,则该参数不是常量:#includetemplatevoidf(constT&arg)//argisn'tconstifTisareferencetype{arg=-1;}intmain(){intx=0;f(x);//instantiatefwithreferencetypestd::cout这是怎么回事?我的猜测是arg的初始类型是int&const&并且它以某种方式转换为int&。如果是这样,那么根据

c++ - 为什么不允许将右值引用绑定(bind)到非 const 引用,但允许在一个上调用非 const 成员函数

就危险性而言,以下内容大致相同,但语言不允许使用后两个,而第一个则不是。#include#includeintmain(){std::vectorv;//allowedstd::vector().swap(v);//notallowedv.swap(std::vector());//notallowedstd::swap(std::vector(),v);}我知道VisualStudio允许所有这些作为编译器扩展通过,我忽略了这个问题。我实际上并不是在争论第一个是不允许的——我实际上更喜欢第二个是允许的(有些地方这会使代码更优雅,通常当C++允许你做一些可能很危险但让它通过可能是有益的

c# - 什么是 C++ const 引用返回值的 c# 模拟

我确实为类成员编写了C++访问器SomeClassconst&x()const{returnm_x;}似乎在C#中这种类型的唯一保护是使用私有(private)(或未定义)集定义属性。但这只能防止分配,不能防止对某些类状态的操纵。旁注:c++允许通过const指针删除m_x-恕我直言,这简直是对标准主体的惊人监督。 最佳答案 现在,在C#7.2中,您可以使用refreadonly来达到同样的目的。您可以查看更多信息here.检查第三点。 关于c#-什么是C++const引用返回值的c#模

c++ - const 指针和 const 数组的输出

当我们有两个运算符用于输出对象和这些对象的数组,并尝试输出常量对象数组时,就会涉及到对象运算符。有没有办法强制数组的运算符与常量对象的c数组一起使用?示例代码:#include#includeusingstd::size_t;namespacenm{structC{inti;};templateusingc_c_array=C[N];inlinestd::ostream&operatorinlinestd::ostream&operatorconst&){returnlhs输出:1211此外,在我的例子中,2运算符使用1作为输出。 最佳答案