Topic: Scope of variables defined within statements (Was: Modifying delete() argument)


Author: joe@bftsi0.UUCP (Joe Foster of Borg)
Date: 1 Nov 93 00:54:12 GMT
Raw View
In article <DOUGM.93Oct30201215@cs.cs.rice.edu>, dougm@cs.cs.rice.edu (Doug Moore) writes:
> In article <2audac$444@nic.lth.se> dag@control.lth.se (Dag Bruck) writes:
>    >if (Derived* d = dynamic_cast<Derived> (b))
>    >  { ... }

>    Doug Moore has missed a non-obvious but significant advantage: in the
>    second example, "d" only exists in the scope of the if-statement if it
>    points to something valid.

> I acknowledge my error.  I should have used "for(int i ..." as an
> example instead.

IMHO, it would be best if i existed only in the scope of the for
statement, as d above exists only in the scope of the if
statement, to maintain consistency. If you want to check i after
the loop completes, you should define it just before the for. It
could even be on the same line as the for, thanks to the lexical
analyzer. :-)

--
Joe Foster (joe@bftsi0.uucp)
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!