Confusing C++ global scope issues -
i taking c++ practice test , i'm confused set of access scope , point of declaration related questions. both questions related each other..i know answers..what need proper explanation :
what value of local variable x @ end of main
int x = 5; int main(int argc, char** argv) { int x = x; return 0; } ans: undefined
what value of y @ end of main?
const int x = 5; int main(int argc, char** argv) { int x[x]; int y = sizeof(x) / sizeof(int); return 0; } answer: 5
from standard: 3.3.1 [basic.scope.pdecl]
the point of declaration name after complete declarator (clause 8) , before initializer (if any), except noted below.
the standard has 2 examples clarify this:
int x = 12; { int x = x; } here second
xinitialized own (indeterminate) value.[note: nonlocal name remains visible point of declaration of local name hides it. [example:
const int = 2; { int i[i]; } declares local array of 2 integers. ]]
these 2 examples cover 2 cases in question.
Comments
Post a Comment