TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALL
from: RAJESH
date: 1997-08-25 22:01:00
subject: Re: Why the braces?

From: rajesh 
Neil Heller wrote:
> case 1 :
>         int bar = 2;
>         break;
>     case 2 :
>         int bar = 3;
the variable 'bar' when defined this way, only exists inside the
innermost braces. You are defining bar
as an automatic variable twice inside one pair of braces. Obviously,
this is an error..
For the same reasons, the next piece of code -will- work because of
different sets of braces ..
>     case 2 : {
>         int bar = 3;
>         break;
>     }
> }
> 
> I was under the assumption that the braces weren't necessary in C++ as
> far as declaring variables is concerned.  
For automatic variables they matter .. a lot
Rajesh
--- ifmail v.2.9
---------------
* Origin: Kalptaru Net - Rajesh's Linux Node - (6:606/30@fidonet)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.