views:

252

answers:

3

Is the following code valid C++?

const int  var = 10;
{ 
   int  var[var]; // why doesn't this give any error ?
}

Note : The code compiles on my g++ compiler.

+8  A: 

Yes the code is valid C++. Non-local var is visible up to the point of declaration of local var.

So int var[var] defines a local array of 10 integers.

Prasoon Saurav
+19  A: 

As-is? No. If it were in the body of a function? Yes.

The first line declares an integer constant named var with a value of 10.

The braces begins a new block. Within that block, a new variable is declared, named var, which is an array of int with a size equal to the value of the integer constant previously declared as var (10).

The key is that var refers to the first variable until after the second variable named var is fully declared. Between the semicolon following the second declaration and the closing brace, var refers to the second variable. (If there was an initializer for the second variable, var would begin to refer to the second variable immediately before the initializer.)

James McNellis
Lol -- +1 for the "in the body of a function" bit :P
Billy ONeal
@Billy: Well, I think that's somewhat important; if this code were at namespace or class scope you'd have to make the block a namespace or class or function or something...
James McNellis
@James McNellis: name hiding, or one of my most hated features. You can spend hours trying to understand what's wrong...
Matthieu M.
@Matthieu: If you're ever in a scenario where names are being hidden, 1. that is easy to see (names are duplicated), and 2. it's an indication that the function or method you're working in is too long. Maybe if you're referring to hiding *inherited* names you'd have a case....
Billy ONeal
@Matthieu: Maybe. I would have agreed two months ago, but recently I've been learning C# and it has very stringent rules concerning when names can be reused (for example, `for (int i = ...) { } int i;` is not allowed, which combined with a few other annoyances has driven me absolutely nuts :-)).
James McNellis
@Billy: It's supposed to be obvious, yet it's easy to use `i`, `it` or `end` and it get very confusing. However Python is even worse in this department with its absence of scoping so it's not that bad. @James: this is crazy, since there's no hiding going on.
Matthieu M.
@Matthieu: Actually, I shouldn't be too critical of that feature; I'm just having a hard time giving up C++ and I'm hypercritical of all things related to managed code :-D. In reality I think what they've done is actually very good: the rule is that you can't reuse a name in nested scopes (so `for (int i = ...) { } for (int i = ...) { }` would be okay but the above example is not). That prevents name hiding and is consistent and easy to remember (how many C++ rules are consistent and easy to remember? :-P).
James McNellis
@James: good point about consistency :)
Matthieu M.
+1  A: 

Yes the code is valid C++ It is a concept of SCOPE : Hiding Names

 const int  var = 10;
  { 
   int  var[var]; // why doesn't this give any error ?
  }

I think this link clears your doubt

IN C++:

http://msdn.microsoft.com/en-US/library/9a9h7328%28v=VS.80%29.aspx

In C :

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fzexscope_c.htm

If u want deep knowledge On this : Go for this link,here the information is about Lexical versus dynamic scoping

http://en.wikipedia.org/wiki/Scope_%28programming%29

but in your code : "Scope ::" of visibility of var .Here It differs like local and non-local variable. Inside braces { x=1; } local variable . where as here {y=1;{x=1;}} ,here it differs.

helpful links

http://msdn.microsoft.com/en-us/library/b7kfh662%28VS.80%29.aspx

http://www.awitness.org/delphi_pascal_tutorial/c++_delphi/c++_scope_variables.html

BE Student
@BE :I think the first 3 links which gave is clear.about this example>The ramining no need
USER