草庐IT

const-string

全部标签

c++ - const 指向成员的指针是否默认指向一个 int?

在玩指向成员的指针时,我遇到了一种似乎有点不一致并且对我来说有点违反直觉的行为。考虑以下虚拟结构:structfoo{intx;doubled;};和以下main():intmain(){intfoo::*ptr=&foo::x;doublefoo::*ptr2=&foo::d;}这里我们没有任何异常-两个指向const成员的指针。代码编译正常。引起我注意的是,当我们添加const时,情况发生了一点变化。考虑以下代码:intmain(){//nointordoubleafterconstconstfoo::*ptr=&foo::x;}代码在GCC8.2.01上编译良好。请注意,我没有指

c++ - 函数调用参数,char * vs string 默认构造函数

在C++11及更高版本中调用函数/方法时,哪个更好(如果有区别)?让我们假设这个函数/方法:voidfunc(std::strings){...}以下哪一个最好?func(std::string())或func("")更一般地说,在初始化或参数传递期间始终显式调用构造函数是否有任何优势? 最佳答案 最好调用默认构造函数,因为它保证不会做任何不必要的工作。当传递一个空字符串文字时,可能是字符串实现做了一些处理该字符串的工作(例如计算它的长度)。空字符串文字不是可以与非空字符串文字区别对待的Elixir。它的类型是constchar[1

c++ - const 重载的 operator[] 函数及其调用

我在类array中定义了两个版本的重载operator[]函数。ptr是指向array对象第一个元素的指针。int&array::operator[](intsub){returnptr[sub];}和intarray::operator[](intsub)const{returnptr[sub];}现在,如果我定义一个const对象integer1只能调用第二个函数......但是如果我创建一个非const对象然后调用如下:cout这里调用了哪个函数? 最佳答案 在您的第二个示例中,将调用非常量版本,因为不需要转换,不需要转换的调

c++ - boost::interprocess::basic_string 作为 std::string

我正在尝试用constboost::interprocess::basic_string&替换返回conststd::string&的类方法。我面临的主要挑战是这两个类之间的不兼容性,尽管它们的实现相似。为了更清楚的解释,我将把它放入代码中classA{std::stringm_str;conststd::string&StrVal(){returnm_str;}}现在这个类看起来像这样:typedefboost::interprocess::allocatorShmemAllocatorChar;typedefboost::interprocess::basic_string,Shm

c++ - ARM C++ - 如何将 const 成员放入闪存中?

我有这个代码classIO{public:IO(LPC_GPIO_TypeDef*port,intpin):_pin(pin),_port(port){};constint_pin;LPC_GPIO_TypeDef*const_port;voidtest(){LPC_GPIO0->FIOSET=0;}};IOled1(LPC_GPIO0,5);intmain(){led1.test();return0;}当我编译它时,我得到了textdatabssdechexfilename65608664298lpc17xx我希望const_port和_pin变量存储在闪存中,因为它们被标记为con

c++ - 将参数作为 const 引用与普通引用传递

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Howcomeanon-constreferencecannotbindtoatemporaryobject?有这样的代码:voidfun_ref(int&par){}voidfun_const_ref(constint&par){}intmain(){//fun_ref(2);error:invalidinitializationofnon-constreferenceoftype‘int&’fromatemporaryoftype‘int’fun_const_ref(2);charvar=3;//fun_

c++ - 用包含 const 成员的结构实例替换堆栈与静态变量

使用gcc4.4.3版本如下:gcc-g-xc++-lstdc++-std=c++98-o./main./main.cppmain.cpp中的这段代码可以正常编译:#includestructA{A():m_flag(false){}constboolm_flag;};staticAaa=A();intmain(intargc,char*argv[]){Aa;//Notstatic=copyOKAb(a);Ac=b;Ad=A();//Static=copynotOK//aa=A();}但是如果我取消注释aa=A();我会得到:./main.cpp:Inmemberfunction'A&

c++ - 指针到指针到 Const 的转换

我正在读一本名为C++Gotchas的书这解释了const指针之间的转换,我在理解以下规则时遇到了一些麻烦:TwopointertypesT1andT2aresimilarifthereexistsatypeTandintegern>0suchthat:T1iscv1,0pointertocv1,1pointerto...cv1,n−1pointertocv1,nTand,T2iscv2,0pointertocv2,1pointerto...cv2,n−1pointertocv2,nTwhereeachcvi,jisconst,volatile,constvolatile,ornoth

c++ - 指向 const 成员函数的非类型模板函数指针

我正在编写一个委托(delegate)类,但它无法采用const成员函数。这是一个测试用例:classfoo{public:voidMemberFunction(){printf("nonconstmemberfunction\n");}voidConstMemberFunction()const{printf("constmemberfunction\n");}};templatevoidCall(C*instance){(instance->*Function)();}intmain(intargc,char**argv){foobar;Call(&bar);Call(&bar);

c++ - forward_list::splice_after( const_iterator pos, forward_list& other, const_iterator i ) 功能

我正在阅读有关此功能工作方式的不同解释。cplusplus.com说这个函数应该“直接在i之后移动元素”。然而cppreference.com表示它拼接元素ATi。MSvisualstudio同意cplusplus.com。但是,实际上正确的行为是什么?我倾向于认为“在i之后”移动更合乎逻辑(&不需要N时间来找到前面的节点)。(PS:没有forward-list标签?) 最佳答案 23.3.4.6voidsplice_after(const_iteratorposition,forward_list&x,const_iterator