On Apr 23, 1:19 pm, siliconmu...@[EMAIL PROTECTED]
wrote:
> Hi, I was wondering what is the point of the language sup****ting
> nested variable declarations of the same name?
> [snip]
> I can't think of a reason why this would be sup****ted, and I feel the
> compiler should throw a warning. So, is there a good useful reason to
> allow this?
Nested variables help decouple the implementation of a function from
the calling context.
Imagine you write 'inline double factorial(int)', put it in a header
file, and tell someone "I've got this great function, try it out".
With your idea above, they would have to change their code to make
sure they didn't have a variable 'i' anywhere in their call stack
before they could call your factorial function....
Put the other way around, if you want to write your factorial(int n)
function, you should be able to create an "int i" inside without
worrying whether there's an int i elsewhere: your function can't
access or change anything of the caller's that the caller doesn't
provide explicit access to, so what's the point in knowing about it?
HTH, Tony
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|