[ Date Index ][
Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]
On Thu, 2003-05-08 at 08:41, Darke, Clive wrote: > The problem here is scoping I think. While it is perfectly legal to declare > a new variable after executable code in C++ (unlike C earlier than C99) that > is not the problem, it is the initialization. In case 0, c is initialized; > in case 1 it is not. The key is that a case statement does not generate a > block of scope automatically, you have to explicitly define one. I think > you probably meant: > > case 0 : > { > int * c = new int; > // .... etc > break; // can be inside or outside this block > } > case 1 : > { > int * d = new int; > break; > } > > ... and this is not because you are new to C++, I showed this to a couple of > other wrinkly coders and they didn't see it either! > > Clive > (wrinkly coder) Oops I placed the {} on my example code for compilation as a matter of habit. I think my subconscious got this one before my conscious mind had a chance to react. I went back to the code I wrote to test the problem this morning and found that I had indeed placed the {} around the statement. I must have been so eager to get back with the GCC problem as to completely ignore the real issue. On my compiler, the following code works if you do g++ <filename> but not gcc <filename>. int main() { int * a = new int; int b; switch (b) { case 0 : { int * c = new int; // int * c; // c = new int; } break; case 1 : { int * d = new int; break; } } return 0; } -- The Mailing List for the Devon & Cornwall LUG Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the message body to unsubscribe.