Converted to compiled code in xx.cpp:
class C { int i; public: C(int i_val = 0) : i(i_val) { } }; int main() { int someval = 2; goto label;
and compiled, as shown in g ++ 4.6.0 on MacOS X 10.6.8, throws the indicated errors:
$ g++ -Wall -Wextra -c xx.cpp xx.cpp: In function 'int main()': xx.cpp:13:1: error: jump to label 'label' [-fpermissive] xx.cpp:11:10: error: from here [-fpermissive] xx.cpp:12:7: error: crosses initialization of 'C x' xx.cpp:19:14: error: jump to case label [-fpermissive] xx.cpp:17:15: error: crosses initialization of 'C x2' $
There is a default constructor for each of the variables x , x2 and x3 .
And the C ++ standard just says that you are not allowed to go into a block after a construction variable. What will work:
class C { int i; public: C(int i_val = 0) : i(i_val) { } }; int main() { int someval = 2; goto label;
With three additional pairs of curly braces, you no longer jump into blocks where variables are declared and initialized, so the code is legal and compiles cleanly on the command line shown earlier.
Jonathan leffler
source share