草庐IT

const-string

全部标签

c++ - 如何将 const 字符串值放入 map

我想创建一个map,std::mapm_mapResponseDesc;我正在使用operator[]在map中附加一个值:m_mapResponseDesc[STATUS_LIMIT]="Limithasbeenexceeded";STATUS_LIMIT类型为enum.我遇到错误:errorC2678:binary'=':nooperatorfoundwhichtakesaleft-handoperandoftype'conststd::basic_string'(orthereisnoacceptableconversion)请指出我做错了什么。我没有得到任何线索。

c++ - swig 对基类 'std::string' 一无所知,忽略

我正在尝试使用swig围绕C++库构建ruby​​包装器。其中大部分似乎都有效,但我有一个问题,我很确定与上述警告有关。看起来我正在包装的类之一是从std::string继承的。我在运行swig时看到上面的警告消息。当我在应该返回字符串的ruby​​对象上调用方法时,我看到了这个SWIG::Type_p_std__string:0x.....我在想我需要解决上面的警告,让它起作用,有什么想法吗? 最佳答案 SWIG提示它不知道std::string类,因此无法为其生成代码。SWIG库std_string.i具有用于将C++字符串映射

c++ - 使用 malloc() 为 const char 字符串动态分配内存

我正在编写一个程序,它从.ini文件中读取一个值,然后将该值传递给一个接受PCSTR(即constchar*)的函数。函数是getaddrinfo()。所以,我想写PCSTRReadFromIni()。要返回常量字符串,我计划使用malloc()分配内存并将内存转换为常量字符串。我将能够获得从.ini文件中读取的确切字符数。这种技术可以吗?我真的不知道还能做什么。以下示例在VisualStudio2013中运行良好,并根据需要打印出“hello”。constchar*m(){char*c=(char*)malloc(6*sizeof(char));c="hello";return(co

c++ - 在头文件中使用 const

在文件.h中:externconstintONE;在文件.cpp中#include"file.h"constintONE=1;在main.cpp中#include#include"file.h"intmain(){std::cout问题:为什么我必须在file.cpp中使用#include"file.h"?有ONE的定义。谢谢 最佳答案 默认情况下,声明为const的变量具有内部链接,就好像它们也被声明为static一样。如果包含header,则extern声明将为它提供外部链接,一切都会好起来的。否则,该定义无法从其他翻译单位获得

c++ - 传递与 const 和非常量引用相同的对象

以下代码使用g++v4.8.1编译并输出45,但它的编译是否保证基于标准?其他编译器会提示吗?#include#includevoidtest(conststd::vector&a,std::vector&b){b[0]=45;}intmain(){std::vectorv(1,0);test(v,v);std::cout我知道函数定义本身并没有错,但是当用同一个对象v调用test时,我有点期待我传递一个警告作为const和非const引用的单个对象。 最佳答案 没有问题,因为编译器将这两个参数视为不同的引用。要理解代码,请考虑以下

c++ - 什么时候在 C++ 程序的开头使用 '#include <string>'?

我对#include的使用感到困惑在程序的开始。例如,在下面的代码中,我不使用#include但该函数在运行时仍会打印出字符串“Johnny'sfavoritenumberis”。#includeusingnamespacestd;voidprintVariable(intnumber){cout但是,在下面的代码中,它确实包含#include.#include#includeusingnamespacestd;classVar{public:voidsetName(stringx){name=x;}stringgetName(){returnname;}private:stringn

c++ - 从 std::stringstream 传递 std::string 引用作为参数

我正在使用std::stringstream构造一个字符串,然后尝试将完成的字符串作为对函数的引用传递,该函数将std::string&作为参数。我在GCC上遇到编译错误:../src/so.cpp:22:21:error:invalidinitializationofnon-constreferenceoftype‘std::string&{akastd::basic_string&}’fromanrvalueoftype‘std::basic_stringstream::__string_type{akastd::basic_string}’../src/so.cpp:12:6:e

c++ - 在哪些情况下 (const) 引用返回值是不安全的?

我倾向于经常使用以下符号:constDatum&d=a.calc();//continuetoused当calc的结果在堆栈上时,这有效,参见http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/.尽管编译器可能会在此处进行优化,但显式避免临时对象感觉很好。今天我意识到,d的内容在数据写入a的成员后变得无效。在这种特殊情况下,get函数只是返回对另一个成员的引用,然而这与write完全无关。像这样:constDatum&d=a.get();//...someoperation.

c++ - 采用 `std::string` 的显式构造函数得到 `char*` 并且工作正常

将StringBuilder构造函数标记为显式我想我不能传入char*但似乎我可以,因为它编译得很好。classStringBuilder{public://StringBuilder(constchar*);explicitStringBuilder(std::strings){}};intmain(){StringBuilders1("hello");StringBuilders2(std::string("hello"));}http://cpp.sh/6uomq 最佳答案 basic_string采用字符指针的构造函数不是e

c++ - 标签调度、可变参数模板、通用引用和遗漏的 const 说明符

请考虑以下示例(标签分发、可变参数模板、完美转发等,全部合而为一):#include#include#includestructA{};structB{};voiddoIt(A&&,conststd::string&){std::coutvoiddoIt(T&&,Args&&...){std::coutvoidfn(Args&&...args){doIt(T{},std::forward(args)...);}intmain(){conststd::stringfoo="foo";std::stringbar="bar";fn(foo);fn(bar);fn(foo);}在这种情况下,