草庐IT

is_const

全部标签

C++制作指向const对象的指针数组

我正在尝试创建一个指向常量对象的非常量指针的非常量数组。我的想法是我应该能够更改数组中的指针指向的内容,但它们指向的是常量对象。我在定义这个数组时遇到了问题(它是指向Person类型对象的指针数组-一个自定义类)。我目前正在这样声明数组:Person*people[10];此外,这并没有明确指出指针指向constPersons。所以当我做这样的事情时:people[i]=&p;其中p是对constPerson类型对象的引用,它失败了。 最佳答案 当有疑问时...使用typedef(因为它是显式的,添加了更多专门的语义并完全避免了混淆

c++ - Qt 5.5 和 Qt Installer Framework 2.0.1 : Logo is not displayed despite being present in config. xml

我已经使用预编译的QtInstallerFramework二进制文件2.0.1版为我的应用程序编写了一个安装程序。但是,安装程序窗口不会以任何可能的方式显示我的任何Logo。有问题的Logo名为“installerlogo.png”,这是一个带有alphachannel的64x64图像,位于安装程序目录结构的顶部(与config/和packages/目录所在的目录相同。)为了图标的目的我又做了一个logo,名字叫“installericon.ico”,就是上面那个的16x16版本,只是简单的重命名为“.ico”(是不是做法不对?)我在config.xml文件中尝试了以下内容:insta

c++ - std::is_signed 不适用于强类型枚举:int

谁能解释一下,为什么#include#includeusingnamespacestd;enumE:signedint{a=-1,b=1,};intmain(){std::cout()std::is_signed不做,锡上写的是什么?谢谢... 最佳答案 如果我们查看is_signed的文档它说:IfTisasignedarithmetictype,providesthememberconstantvalueequaltrue.Foranyothertype,valueisfalse.并且枚举不是算术类型,因此结果应该为假。来自C+

c++ - "-ftrapv"和 "-fwrapv": Which is better for efficiency?

来自GNU的网站:-ftrapvThisoptiongeneratestrapsforsignedoverflowonaddition,subtraction,multiplicationoperations.-fwrapvThisoptioninstructsthecompilertoassumethatsignedarithmeticoverflowofaddition,subtractionandmultiplicationwrapsaroundusingtwos-complementrepresentation.Thisflagenablessomeoptimizationsa

c++ - 为什么 std::is_array 对 std::array 返回 false?

自std::array和std::is_array都是在C++11中引入的,编译失败似乎很奇怪:#include#includestatic_assert(std::is_array>::value);有没有一种简单的方法来检查某物是否是一个数组,包括T[N]的两种可能性?和std::array? 最佳答案 std::is_array仅对看起来像T[]的类型定义为真或T[N].std::array不包括在内。您不能修改或专门化std::is_array成为true_type对于std::array在标准之下;这会使您的程序格式错误,

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++ - 为什么 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++ - 为什么 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++ 错误 : Type Name is Not Allowed

我正在尝试学习指针参数中的新类(class),我想让函数senior和everyoneElse接受指针x,但是当我尝试使用指针pAge调用函数时,它显示错误:类型名称是不允许的。怎么了?#includeintsenior(int*x);inteveryoneElse(int*x);usingnamespacestd;intmain(){intage(0);int*pAge(&age);cout>age;if(age>59)senior(int*pAge);elseeveryoneElse(int*pAge);return0;}intsenior(int*x){return*x;}int