草庐IT

c++ - 为什么不允许 "inlined"静态常量,整数除外?

PossibleDuplicateWhycan'tIhaveanon-integralstaticconstmemberinaclass?structExample{staticconstintOne=1000;//LegalstaticconstshortTwo=2000;//IllegalstaticconstfloatThree=2000.0f;//IllegalstaticconstdoubleFour=3000.0;//IllegalstaticconststringFive="Hello";//Illegal};#2、#3、#4和#5是非法的吗?我想我知道#5的原因:编译器

c++ - STL 迭代器和 'const'

当我使用迭代器时,我遇到了一个问题,似乎是对const的某种隐式转换。我不太确定哪个代码是相关的(如果我知道我可能不会问这个问题!)所以我会尽力说明我的问题。typedefsetContainer;//notconstvoidLargeObject::someFunction(){//notconstContainer::iteratorit;//notconstfor(it=c.begin();it!=c.end();++it){//assumecisa"Container"(*it).smallObjectFunction();//notaconstfunction}}但是我总是得

c++ - 没有 () 的访问器,或对成员变量的 const 引用

我有兴趣创建一个我可以像这样使用的类classMyClass{vectorm_vec;public://Eitherthisconst&vectorvec;//Orsomeversionofthis.const&vectorgetVec(){returnm_vec};MyClass():vec(m_vec){}voidchangeVec(){m_vec.push_back(5);}}现在如果我想使用getVec(),语法有点麻烦:myClass.getVec()[5]我更希望能够以某种方式使用myClass.vec[5]不公开修改vector的能力。IE,我希望成员变量是私有(priv

c++ - 将 operator== 重载为带有模板参数的自由函数的语法是什么?

我有一组多态类,例如:classApple{};classRed:publicApple{};classGreen:publicApple{};以及比较它们的自由函数:booloperator==(constApple&,constApple&);booloperator我正在设计一个可复制的包装器类,它将允许我使用类Red和Green作为STL映射中的键,同时保留它们的多态行为。templateclassCopy{public:Copy(constCat&inCat):type(inCat.clone()){}~Copy(){deletetype;}Cat*operator->(){

C++ const 语义引用

如果代码中有如下内容:func(constbase&obj)const语义是什么意思?这里的常数是什么?obj是对非常量对象的const引用还是对const对象的非常量引用? 最佳答案 没有“非常量”引用这样的东西,也就是说,引用总是绑定(bind)到同一个对象,而且没有办法改变它。"consttype&"表示对const类型的引用。 关于C++const语义引用,我们在StackOverflow上找到一个类似的问题: https://stackoverflo

c++ - 尝试使用 std::add_const 将 T& 转换为 const T&

我有一个T&,它有一个函数的const和非常量版本。我想调用该函数的const版本。我尝试使用std::add_const将T&转换为constT&但它不起作用。我做错了什么,我该如何解决?这是一个简单的例子。voidf(int&){std::cout::type>(r));}输出:int& 最佳答案 类型特征是解决这个问题的一种非常费力的方法。只需使用模板类型推导:voidf(int&){std::coutconstT&make_const(T&t){returnt;}intmain(){inta=0;int&r=a;f(make

c++ - 如何检查对象是否为常量?

我的问题是我不知道如何检查对象是否为常量。我只能使用C++98。如何检查对象是否具有const修饰符?如何正确重载函数?intmain(){Vecx;constVecy;cout我需要这样的输出:x是常量吗?不x是常量吗?不你是常量吗?是的你是常量吗?是的我用过:voidVec::IsConst(Vecconst&vecc)const{std::cout 最佳答案 constness是已知的,仅在编译时使用,该信息在运行时不存在,没有任何意义。然而,在编译时,如果你有一个C++11兼容的编译器,你可以使用标准的std::is_con

c++ - 使 std::unique<T> 与 std::unique<const T, CustomDeleterType> 兼容

在代码中,我为特定对象定义了3个std::unique_ptr指针类型:typedefstd::unique_ptrnonConstPtrDefaultDelete;typedefstd::unique_ptr>nonConstPtrCustomDelete;typedefstd::unique_ptr>ConstPtrCustomDelete;我遇到了一个用例,我需要将nonConstPtrDefaultDelete转换为ConstPtrCustomDelete并将nonConstPtrCustomDelete转换为ConstPtrCustomDelete。换句话说:nonConst

c++ - 模板中的 "const const T"

以下代码可在G++4.4.0和MSVC2008Express上编译和运行。#includetemplatestructA{protected:Tv;public:constTget()const{returnv;}A(Tv_):v(v_){}};classB:publicA{public:voiddoSomething()const{constint*tmp=get();std::cout(p){}};intmain(intargc,char**argv){inta=134;Bb(&a);constB&c=b;b.doSomething();c.doSomething();return

c++ - 错误 : request for member '..' in 'this' , 是非类类型 '--* const'

这是我的第一个问题。请原谅,我刚刚进入C++并开始使用DS。堆叠!!!我的代码:我认为usingnamespacestd;typedefcharstackElement;classStack{public:stackElement*contents;//dynamicallyallocated:aswedonotknowwhatwouldbethesizeofourarray.inttop,maxSize;//currentTopindexinthearray//maxsizeofthearray;weneedittoknowifthearrayisfullStack(intmaxSi