【问题标题】:Making sense of where "const" goes in a declaration理解“const”在声明中的位置
【发布时间】:2011-09-29 06:26:30
【问题描述】:

我很难找到一个直观的模式来说明 const 在 C 和 C++ 语言的声明中的使用方式。以下是一些示例:

const int a;    //Const integer
int const a;    //Const integer
const int * a;  //Pointer to constant integer
int * const a;  //Const pointer to an integer
int const * a const;    //Const pointer to a const integer

在第 1 行和第 2 行中,const 似乎可以出现在 int 之前或之后,这是它所修改的。

  1. 那么,在第 4 行,编译器如何确定 const 正在修改 *(指针)而不是 int
  2. 编译器遵循什么规则来决定const 适用于哪个事物?
  3. * 是否遵循相同的规则?

【问题讨论】:

  • 必须链接到clockwise spiral rule
  • 如果不涉及数组或函数,简单地从右到左读取也可能有所帮助:int const * const a: "a 是一个指向 const 的 const 指针整数”。
  • 马克,如果你提交了这个作为答案,我会检查它。非常有用,真的能帮我弄清楚!
  • @Adam 下面的答案提供了极好的解释,我不觉得只是链接提供了真实的答案;不过,很高兴它有所帮助。
  • 我一直不明白为什么“顺时针螺旋”规则是顺时针螺旋。您正在阅读一维令牌字符串,因此您可以通过“向下”而不是“向上”轻松地逆时针绘制想象中的螺旋......

标签: c++ c syntax constants


【解决方案1】:

假设你总是把const放在类型的右边,你可以从右到左把一个变量声明读成一个句子:

int const x;        // x is a constant int
int *const x;       // x is a constant pointer to an int
int const *x;       // x is a pointer to a constant int
int const *const x; // x is a constant pointer to a constant int

如果您将const 放在类型的左侧,这仍然有效,但需要更多的脑力劳动。请注意,这同样适用于指向指针的指针(和更高阶的结构):

int *const *const x; // x is a constant pointer to a constant pointer to an int

【讨论】:

  • 所以基本规则是从右到左阅读,如果最左边的东西是const,那么它直接适用于它右边的东西。所以const适用于左边的东西,除非左边什么都没有,所以它适用于右边的东西。
【解决方案2】:

编译器一般从右到左读取类型,所以:

T const& const

应该读作:

 const (a constant)
 & (to a reference)
 const (to a constant)
 T (of type T)

所以,基本上关键字“const”会修改它之前的所有内容。但是,“const”先出现的情况有一个例外,在这种情况下,它直接修改它右侧的项目:

const T& const

以上内容读作:

const (a constant)
& (to a reference)
const T (to a constant of type T)

而上面等价于T const& const。

虽然编译器是这样做的,但我真的只是建议记住“T”、“const T”、“const T&”、“const T*”、“const T& const”、“const T* const”的情况”、“T& 常量”和“T* 常量”。您很少会遇到“const”的任何其他变体,当您遇到时,使用 typedef 可能是个好主意。

【讨论】:

  • 感谢您解释编译器是如何做到的,而不仅仅是“人类可以做到”,这正是我所要求的。
  • 我相信没有所谓的“恒定引用”(或者,换句话说,它们总是恒定的)。所以T & const 在语义上是不正确的。 VC++2010 给出警告并忽略const。只是提醒。 :)
  • -1 是坏的例子。您应该在这里使用指针,而不是引用。
【解决方案3】:

对于指针,这是我从 Scott Meyers 的一本书中挑选的(我认为)。通过 * 画一条垂直线,然后与 const 关键字位于同一侧的任何内容都是 const。

澄清一下:

int * const a 表示 a 是 const,而不是 int。而“a”是指向(非常量)int 的指针。

【讨论】:

    【解决方案4】:

    嗯,首先,这个问题更多的是个人喜好。对于休闲程序员来说,更多的是个人风格。但是,对于那些与企业打交道的人来说,他们可能会为程序员制定某种编码约定,以便他们遵循这样的方式,以便每个人都能获得一致的编码风格。

    (1) 对于 const int * const a;,这意味着您无法更改指针指向的内容,但可以修改该内存位置。

    (2) 'const' 由作为程序员的您决定,您是希望指针指向的地址保持不变还是希望指针不修改它指向的指针。

    (3) 是的,* 的规则与const int * const a; 的规则相同

    作为附加说明,您的最后一行不是有效的 C89。

    希望对您有所帮助。干杯!

    【讨论】:

    • 这可能是程序员的偏好,但绝对不是其他必须阅读他的代码的人!
    • 问题不仅在于写它们,还在于阅读别人写的。
    【解决方案5】:

    理解这些的关键是要意识到* 绑定到a,而不是类型。所以你应该把它们读成:

    const int a;    // a has type const int
    int const a;    // a has type int const
    const int * a;  // *a has type const int
    int * const a;  // *(const a) has type int, which means *a has type int and a is const
    int const * a const;    // *(const a) has type int const, which means *a has type const int and a is const.
    

    (请注意,尽管 C++ 引用不遵循此规则)。

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      相关资源
      最近更新 更多