🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2033

Scope of variables declared in if/while/for

#2033
From: Robert Martin
Date: Mon, 25 Sep 1995 16:56
51 lines
1890 bytes
In reading the WP I see that the scope of variables declared in 'for'
statements is now identical with the scope of variables declared in
'if' and 'while' statements.  i.e. any variable declared in a 'for',
'if' or 'while' statement is local to the statement, and to the
controlled statement, but not to the enclosing scope.

e.g.

if ((Circle* c = dynamic_cast<Circle*>(s)) != 0)
{
   // c is local to this scope.
}

// c is unknown to this scope.

-------------------------

And the same for:

for (Iterator<Shape*>i(s); i; i++)
{
  // i is local to this scope
}
// i is unknown in this scope.

------------------------------

Do I have this right?  If so, doesn't this break a lot of old C++
programs that depended upon variables declared in the 'for' statement
being local to the enclosing scope.

e.g.  the following used to be legal, but now is illegal.

for (int i=0; i<100; i++)
{
  // expressions using i;
}

if (i == 100) // loop ran to end.
-- 
Robert Martin       | Design Consulting   | Training courses offered:
Object Mentor Assoc.| rmartin@oma.com     |   OOA/D, C++, Advanced OO
2080 Cranbrook Rd.  | Tel: (708) 918-1004 |   Mgt. Overview of OOT
Green Oaks IL 60048 | Fax: (708) 918-1023 | Development Contracts.

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. 
]
Technical Details
Message-ID: <MATT.95Sep25095657@physics2.Berkeley.EDU>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: rmartin@rcmcon.com (Robert Martin)
Newsgroups: comp.std.c++
Subject: Scope of variables declared in if/while/for
Date: 25 Sep 1995 16:56:57 GMT
Organization: R. C. M. Consulting Inc. 708-918-1004
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <MATT.95Sep25095657@physics2.Berkeley.EDU>
NNTP-Posting-Host: physics2.berkeley.edu