当然它必须抛出一个错误。
{和大}括号用于定义一个块,该块为该块提供一个新的scope. 因此,在一个范围内定义或创建的所有东西都不能在该范围之外访问。
但是,如果该块包含其他一些块,则您可以访问该块中外部范围的成员。
IE
int main()
{
int a;
{
int b;
{
int c;
b = c; // `b` is accessible in this innermost scope.
a = c; // `a` is also accessible.
}
// b = c; `c` is not accessible in this scope as it is not visible to the 2nd block
b = a; // `a` is visible in this scope because the outermost block encloses the 2nd block.
}
// a = b; outermost block doesn't know about the definition of `b`.
// a = c; obviously it is not accessible.
return 0;
}
而且,由于{}在if's 、、for和构造中使用,它们在使用时为它们中的每一个定义了一个新的范围。whiledo-whileswitch
data这是一种很好的机制,您可以在其中限制成员的可见性,C其中的definition/declaration变量只允许在遇到任何可执行语句之前的块开头。