D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

RE: [LUG] A programming problem



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)
	

-----Original Message-----
From: Tom Glare [mailto:tom@xxxxxxxxxxxxxxxxxxxx]
Sent: Wednesday, May 07, 2003 9:26 PM
To: list@xxxxxxxxxxxx
Subject: [LUG] A programming problem


Are there any Charlie Double Plus programmers who could explain this
little conundrum to me ?

When I compile the following programme

===

int main() {
   int * a = new int;
   int b;
   switch (b) {
      case 0 :
         int * c = new int;
         // int * c;                  Compiles sucessfully
         // c = new int;              if I do it this way
         break;
      case 1 :
         int * d = new int;
         break;
   }
   return 0;
}
===
- I get the following error :
===

test.cpp: In function `int main ()':
test.cpp:10: jump to case label
test.cpp:6:   crosses initialization of `int *c'

===

Am relatively new to the wonderful world of C++, so is probably something
elementary I am getting wrong.


As an quick aside - and note that I am only adding this to an eMail that I
was going to send anyway, so excuse myself from blacking the kettle - I
would like to say that I hope Ray (Smith) is not put off the LUG by the
rudeness of one or two subscribers. I certainly do not mind the occasional
eMail that is possibly misplaced but certainly well intentioned, but I do
not like to read the public whinings of those who could have easily just
made a gentle suggestion in a private eMail to Ray. Any complaints about
my view, please complain to me, not to the list, unless you really want to
prove my point for me.


Regards to all, Tom.



--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly