用常量字符串和字符串常量声明字符串有什么区别
What is the difference between declaring a string with const string and string const
>我正在浏览网上的一些代码,我看到了这两个声明,
const std::string strFoo = "MyFoo";
和
std::string const strBar = "MyBar";
我对 const 关键字的不同位置感到困惑。它的目的究竟是什么?
谢谢!
在这种情况下,
它没有区别。
对于更复杂的声明,可能会有很大的不同。
你可以说const
适用于左边的东西,如果那里什么都没有,它适用于右边的东西。
例如const
指向 int 的指针上使用限定符:
const int* ptr1; // (1.) pointer to const int
int const * ptr2; // (2.) same as 1.
int* const ptr3; // (3.) const pointer to int
const int* const ptr4; // (4.) const pointer to const int
int const * const ptr4; // (5.) same as 4.
有关如何读取复杂类型声明的更多信息,请参阅以下内容: C 左右规则
相关文章:
- 将错误作为从字符串常量到"char*"的已弃用转换 [-Wwrite-strings]
- 3 与错误最接近的总和:字符串常量之前的预期非限定 id
- 为什么字符串(常量字符* s,size_t pos,size_t len = npos)有效?
- 指向字符串常量的指针
- 警告:ISO C++禁止将字符串常量转换为'char*' [-Wwrite-strings]
- 为什么在波纹管程序中发生了从字符串常量到'char*'的警告已弃用的转换
- 一种创建将字符串常量返回给枚举的类的廉价方法,反之亦然
- 如何从字符串常量创建字符数组
- 将字符串常量转换为char
- C++ 字符串常量和静态初始化顺序惨败
- 使用 std::string 返回开关大小写块中的字符串常量
- 消除从字符串常量到'char*'的已弃用转换的最佳方法"
- 警告:ISO C++禁止将静态“constexpr char*”数据成员的字符串常量转换为“char*”
- 字符常量或字符串常量
- Swig:如何类型映射c ++字符串常量和python字符串?
- 字符串常量之前的预期构造函数、析构函数或类型转换
- 为什么从字符串常量到"char*"的转换在 C 中有效,但在C++中无效
- 警告:已弃用从字符串常量到 'char* 的转换
- 无法获得通过C++警告:(和崩溃后)已弃用从字符串常量到"char*"的转换 [-Wwrite-strings]
- 错误:字符串常量 extern "a" { 之前应为非限定 ID