www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/02/03/09:43:41

Xref: news2.mv.net comp.os.msdos.djgpp:754
From: mbbad AT s-crim1 DOT dl DOT ac DOT uk (I. Badcoe)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: v2 vs v1 C++ interpretation
Date: 1 Feb 1996 10:03:19 GMT
Organization: Daresbury Lab, Warrington, U.K.
Lines: 73
Distribution: world
Message-ID: <4eq357$ruq@mserv1.dl.ac.uk>
References: <4eads7$4e6 AT trog DOT dra DOT hmg DOT gb>
NNTP-Posting-Host: s-crim1.dl.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

itmiller AT taz DOT dra DOT hmg DOT gb wrote:

: Does anyone know why the C++ function...

1	: void term();
2	: int curr_tok = 0;
3	
4	: void term()
5	: {
6	: 	switch (curr_tok)
7	: 	{
8	: 	case 1:
9	: 		double d = 3.0;
11	: 		break;
12	: 	default:
13	: 		return;
14	: 	}
15	: }

Hi,
	I don't think this is correct code.

	The reason is that the variable 'd' is declared within
the block starting on line 7.  So it's a valid symbol anywhere
in that block.  In the default case of the switch, however, you
pass by that declaration.  So any code within the block but after
that point would not know whether there was a variabel 'd' or not.

You'd have to do either:

1       : void term();
2       : int curr_tok = 0;
3
4       : void term()
5       : {
7a	:	double d;
6       :       switch (curr_tok)
7       :       {
8       :       case 1:
9       :               d = 3.0;
11      :               break;
12      :       default:
13      :               return;
14      :       }
15      : }

which puts the variable in the scope of the whole construct, or,

1       : void term();
2       : int curr_tok = 0;
3
4       : void term()
5       : {
6       :       switch (curr_tok)
7       :       {
8       :       case 1: {
9       :               double d = 3.0;
11      :               break;
11a	:	}
12      :       default:
13      :               return;
14      :       }
15      : }

which definitely removes it from the scope of the 'default' code.

Neither version does anything, of course ;-)

	Badders

p.s:	It shouldn't work under straight C, either ?


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019