草庐IT

const-string

全部标签

c++ - 无法将 const 应用于 typedef 引用

以下代码在将const应用于value_type&的返回值引用时有效,但如果我使用相同类型的typedef,则会出错。举个例子:classT{};classA{public:typedefTvalue_type;typedefvalue_type&reference;//Notworkingconstreferenceoperator*()const;//Butthisworks?//constvalue_type&operator*()const;};//Error!consttypenameA::referenceA::operator*()const{}intmain(){ret

c++ - 使临时变量的 const 引用成员安全吗?

我曾多次尝试这样编写代码:structFoo{doubleconst&f;Foo(doubleconst&fx):f(fx){printf("%f%f\n",fx,this->f);//125125}doubleGetF()const{returnf;}};intmain(){Foop(123.0+2.0);printf("%f\n",p.GetF());//0return0;}但它根本不会崩溃。我还使用valgrind来测试程序,但没有出现错误或警告。因此,我假设编译器自动生成了一段代码,将引用指向另一个隐藏变量。但我真的不确定。 最佳答案

c++ - 由于对具有 std::string 的方法的 undefined reference ,链接 webrtc-native 时出错

我正在尝试构建webrtc版本62,使用以下内容1.gitcheckout-bbranch62refs/remotes/branch-heads/622.gngenout_release_62/x64/Debug--args="rtc_include_tests=falsertc_use_h264=falseuse_rtti=trueis_component_build=falseenable_iterator_debugging=falseenable_nacl=falsetarget_os=\"linux\"target_cpu=\"x64\"is_debug=true"3.nin

c++ - VS 2017 不会隐式地将 const char* 转换为 char*

这个问题在这里已经有了答案:Conversionfromstringliteraltochar*isdeprecated[duplicate](2个答案)关闭4年前。我最近安装了VS2017,遇到了一个奇怪的问题。基本上,如果不明确地将它们转换为(char*),我就不能使用硬编码字符串。如果我说类似Function("test")的话,它只会抛出一个错误,指出constchar*与char*不兼容。我真的不想坚持使用VS2015:(。有人知道如何让VS认识到它们是同一回事吗?非常感谢。

c++ - std::string erase 删除最后一个字母

我想为游戏输入一些内容,然后在按退格键后删除字符串中的最后一个字母。我不确定是否应该执行text.end-1或+1到end来执行此操作:if(GetAsyncKeyState(VK_BACK))text.erase(text.end-1,text.end); 最佳答案 std::string实际上有一个pop_back()方法!所以你可以这样做:if(GetAsyncKeyState(VK_BACK)&&!text.empty()){text.pop_back();} 关于c++-std

c++ - 如何在编译时解析静态常量 std::string?

我的C++代码中有一些带有绑定(bind)的SQL查询,这些查询是staticconststd::string,因为这些查询很复杂,所以很容易在某些细节上出错。我想在编译时做一些非常基本的检查,例如计算逗号或:字符的数量。 最佳答案 你不能。staticconststd::string在编译时不存在。constexpr函数可以使用字符串文字,但不能使用std::string对象。 关于c++-如何在编译时解析静态常量std::string?,我们在StackOverflow上找到一个类似

c++ - "cannot convert ' 这个从 'const hand' 到 'hand &' 的指针是什么意思? (C++)

当我尝试这样做时出现错误friendstd::ostream&operatorhand是我创建的类,show是std::ostream&hand::show(std::ostream&os,consthand&obj){returnos其中display声明为chardisplay[6]。有人知道这个错误是什么意思吗? 最佳答案 你需要让hand::show(...)成为一个const方法;向它传递obj引用是没有意义的——它已经将其作为“this”指针接收。这应该有效:classhand{public:std::ostream&s

c++ - 如何在 QLineEdit 中使用 std::string?

我有以下问题。我正在尝试将我编写的大型代码与Qt界面集成。我的一些函数返回std::string。我没有成功地让QLineEdit::setText接受它们(其他返回char的函数不会给我带来问题)。我该怎么办?谢谢!朱塞佩 最佳答案 试试这个:std::stringa="aaa";lineEdit->setText(QString::fromStdString(a));您将需要支持STL的Qt。 关于c++-如何在QLineEdit中使用std::string?,我们在StackOve

c++ - 可以在构造函数中分配 std::string 吗?

我有以下构造函数:TCPConnector(int32_tfd,stringip,uint16_tport,vector&protocolChain,constVariant&customParameters):IOHandler(fd,IOHT_TCP_CONNECTOR){_ip=ip;_port=port;_protocolChain=protocolChain;_closeSocket=true;_customParameters=customParameters;}我想知道是否可以在构造函数中安全地分配一个字符串(即_ip)而无需显式初始化它? 最

c++ - boost::shared_ptr<const T> 到 boost::shared_ptr<T>

我想从boost::shared_ptr中转换常量,但我boost::const_pointer_cast不是答案。boost::const_pointer_cast想要一个constboost::shared_ptr,不是boost::shared_ptr.让我们放弃强制性的“你不应该那样做”。我知道...但我需要这样做...那么最好/最简单的方法是什么?为了清楚起见:boost::shared_ptrorig_ptr(newT());boost::shared_ptrnew_ptr=magic_incantation(orig_ptr);我需要知道magic_incantation