C ++中的声明点 [英] Point of declaration in C++

查看:82
本文介绍了C ++中的声明点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有输出 101 ,而我将以前的 x 分配给新的 x

  int x = 101; 
{
int x = x;
std :: cout<< x<< std :: endl;
}

输出(垃圾):


422634


我认为第二个 x 将初始化为 101 ,但未初始化。



注意:在这种情况下,解决方案是 int x = :: x ,但问题是为什么发生。

解决方案

声明点




名称的声明点为在其完整的
声明器之后和其初始化器之前... [C ++标准§3.3.2 / 1]


编译器



上面的代码等于下面的代码:

  int x = 101; 
{
int x;
x = x; < ------------------ //自赋值,赋一个不确定的值。
std :: cout<< x<< std :: endl;
}

因为,内部 x = (分配)之前完成

  int x = X; <-//现在,我们有了新的`x`,它可以隐藏较旧的
^ //,因此它会将自己分配给自己
|
+ --- //声明点,
// //编译器知道声明x的所有内容。
//然后声明它。



另一方面,当我们声明复杂对象时,声明的重点就更远了。因此,行为是不同的。



例如,下面的代码可以确定

  const int i = 2; 
{
int i [i];
^
|
+ ---- //声明点
//编译器必须到达]
//因此,在将`i`声明为数组
//之前只是一个`i`,`const int i = 2`的`i`
}

在上面的代码中,编译器必须知道数组的实际大小才能完成声明,因此声明的要点是] 。因此, [i] 中的 i 是外部的 i 因为 int i [... i 的声明尚未完成。因此,它声明一个包含 2 元素的数组( int i [2]; )。



 



此外,此示例显示了枚举器的声明点

  const int x = 12; 
{
枚举{x = x};
^
|
+ --- //声明点
//编译器必须到达},然后
//只有一个`x`,即const int的`x` x = 12`

}

枚举器 x 用常量 x 的值初始化,即 12


Why isn't the output 101 while I assigned the previous x to the new x?

int x = 101;
{
    int x = x;
    std::cout << x << std::endl;
}

Output (garbage):

422634

I thought the second x would be initialized to 101 but it isn't initialized.

Note: The solution in this case is int x = ::x but the question is why it happens.

解决方案

Point of declaration

The point of declaration for a name is immediately after its complete declarator and before its initializer... [C++ Standard § 3.3.2/1]

Compiler completes the declaration when it knows enough about declarator.

Above code is equal to the below one:

int x = 101;
{
  int x;
  x = x; <------------------// Self assignment, assigns an indeterminate value.
  std::cout << x << std::endl;
}

Because, the declaration of inner x completed before = (assignment)

int x = x; <--// Now, we have the new `x` which hides the older one, 
     ^        // so it assigns itself to itself
     |
     +---// Point of declaration,
         // here compiler knows everything to declare `x`.
         // then declares it.

 

On the other hand, when we declaring complex objects, the point of declaration is farther. So, the behavior is different.

For example, below code is OK

const int i = 2;
{
  int i[i];
         ^
         |
         +----// Point of declaration
              // compiler has to reach to "]"
              // therefore before declaring `i` as an array
              // there is just one `i`, the `i` of `const int i=2`
}

In above code, compiler has to know the actual size of the array to complete the declaration, so the point of declaration is ]. Therefore the i within [i] is the outer i because declaration of the i of int i[... isn't completed yet. Thus, it declares an array with 2 elements (int i[2];).

 

Also, this example shows the point of declaration for an enumerator

const int x = 12;
{
  enum { x = x };
               ^
               |
               +---// Point of declaration
                   // compiler has to reach to "}" then
                   // there is just one `x`, the `x` of `const int x=12`

}

The enumerator x is initialized with the value of the constant x, namely 12.

这篇关于C ++中的声明点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆