草庐IT

const-char

全部标签

C++ 重载解析、转换运算符和 const

在这种情况下voidf(int*);voidf(constint*);...inti;f(&i);情况很清楚-f(int*)被调用,这似乎是正确的。但是,如果我有这个(这是错误地完成的(*)):classaa{public:operatorbool()const;operatorchar*();};voidfunc(bool);aaa;func(a);operatorchar*()被调用。我不明白为什么这样的决策路径会比使用operatorbool()更好。有什么想法吗?(*)如果将const添加到第二个运算符,代码当然会按预期工作。 最佳答案

c++ - BOOST_TYPEOF 返回 int 而不是 const int

你能解释一下为什么打印1吗?BOOST_TYPEOF不应返回constint。如何在不使用C++11功能的情况下检查函数是否返回const?#include#include#includeconstintf_const_int(){return1;}intmain(){typedefBOOST_TYPEOF(f_const_int())type;std::cout::value) 最佳答案 如果纯右值表达式的类型为cvint,则忽略该cv限定符。[表达式]/6:Ifaprvalueinitiallyhasthetype“cvT,”w

c++ - 在 C++ 中使用 const ArrayType 或 ConstArrayType typedef 具有 const 元素的数组

我将定义一些具有固定大小和常量元素的数组。我尝试使用typedef,但似乎有些困惑:typedefintA[4];typedefconstintCA[4];constAa={1,2,3,4};CAca={1,2,3,4};a[0]=0;ca[0]=0;a=ca;ca=a;所有赋值都会导致上面代码中的语法错误,我认为a[0]=0;在我测试之前应该是合法的。考虑指针,结果更容易理解p[0]=0;和cp=p;是正确的。typedefint*P;typedefconstint*CP;constPp=newint[4]{1,2,3,4};CPcp=newint[4]{1,2,3,4};p[0]=

c++ - 如果我将类声明为 const...而不是 constexpr,程序将无法运行?

这是我的程序:#includeclassDebug{public:constexprDebug(boolh):hw(h){}constexprboolany(){returnhw;}private:boolhw;};intmain(){constDebugx(true);constexprboolf=x.any();}这会引发错误“‘x’的值在常量表达式中不可用”。如果我更换constDebugx(true);与constexprDebugx(true);然后一切正常。我的印象是将constexpr放在对象定义之前与“隐式使该变量成为常量,同时验证它是常量表达式变量”同义。这对我来说表

c++ - Atomineer:确定返回类型是否为const

我正在使用Atomineer9.00格式化我的代码注释,但我很难确定返回类型是否为const。documentation说明对于MethodReturns,使用%type%将提供完整的返回类型(给出的示例是:constint*):MethodReturnsRulesusedtogeneratethedescriptionforthereturnvaluesformethods(VBfunctions).Thefollowingspecialvariablescanbeusedinthissection:%type%Thereturntypeforthemethod(constint*)

c++ - 使用带有 char * 的正则表达式迭代器

我正在尝试将文件读入缓冲区,然后使用正则表达式迭代器。我知道我可以将C++字符串迭代器与正则表达式迭代器一起使用(构造函数是std::regex_iterator),但我想避免将缓冲区复制到字符串中并继续使用低级函数来读取文件(现在我使用open()和read())。structstatbuff;intfile=open(argv[1],O_RDONLY);if(!file)cout我认为我的选择是找到一种使用read()的方法使用C++字符串而不是char*或在char数组上使用正则表达式迭代器的方法。我可以写一个,但我也在努力让我的程序尽可能小。有什么办法可以做到吗?如何将C++字

一文总结 C++ 常量表达式、constexpr 和 const

TLDR修饰变量的时候,可以把constexpr对象当作加强版的const对象:const对象表明值不会改变,但不一定能够在编译期取得结果;constexpr对象不仅值不会改变,而且保证能够在编译期取得结果。如果一个const变量能够在编译期求值,将其改为constexpr能够让代码更清晰易读。constexpr函数可以把运行期计算迁移至编译期,使得程序运行更快(但会增加编译时间)。但如果constexpr函数中存在无法在编译期求值的参数,则constexpr函数和普通一样在运行时求值,此时的返回值不是常量表达式。1.常量表达式和constexprC++11中引入了constexpr关键字。c

c++ - string s怎么改成char * a[]?

我要转型字符串s="aaa,bbb,ccc"进入:char*a[]={"aaa","bbb","ccc"}你能帮我编写程序来处理这个过程吗?我会尝试这样编程:strings="aaa,bbb,ccc";char*a[];charid[]="";strcpy(id,s.c_str());constchar*split=",";char*p;p=strtok(id,split);while(p!=NULL){inti=0;printf("%s\n",p);a[i]=p;i++;p=strtok(NULL,split);}我哪里错了?谁能指点一下? 最佳答案

c++ - 为什么我构造的临时对象 const 不可变?

我有一个类K,我正在调用函数test时构造一个对象。所以我相信构造的K被称为r-value。(这是真的吗?)但令我感到困惑和烦恼的是K对象显然是const,而不是可变。我不想这样。简单测试程序:#includeclassK{};std::stringtest(K&k){return"mutable";}std::stringtest(constK&k){return"const";}intmain(intargc,constchar**argv){std::cerr输出:KconstructedforfunctionargumentisconstKconstructedforlocal

c++ - 创建一个字符串如何改变 const char* 指向的值?

我编写了一个函数,它接受一个字符串并返回一个constchar*,其中包含该字符串的编码版本。我调用这个函数,然后创建一个新的字符串。这样做时,我无意中更改了指向我的constchar*的值,我认为这是不可能的。但是,当我不使用我自己的函数,而只是将一个值硬编码到我的constchar数组中时,当我创建一个字符串时该值不会改变。为什么这里有区别,为什么我仍然能够更改constchar数组的值?#include#include#include#include#includeusingnamespacestd;//returns"@username@FIN"constchar*encode