NikiTo said:
@joel I would not name two arrays in the same way. I consider this to be an error. The compilers should at least issue a warning in my opinion.
No, it's rarely a source of accident. First, replace ‘array’ with more general ‘variable’ (or type) and then think about this example:
struct Rect
{
int width, length;
Rect (const int width, const int length) : length(length)
{
this→width = width;
}
};
Ofc. you will only use one of the two shown ways to set the member variables, but just pick what you like more.
And why would you want to think of another variable name just to prevent any confusion, if coming up with variable names is so hard? :D
You will agree it is nice we do not have to.
Next, an array is just a pointer, so ofc. we we do not want to make this a special case and any different from the above.
But there is a similar case which is indeed a flaw:
for (int i=0; i<10; i++)
{
SomeFunction(i);
}
for (int i=0; i<20; i++)
{
SomeDifferentFunction(i);
}
Depending on compiler (settings), the second declaration of i can cause a compile time error ‘i already defined’.
It seems there is (or was) no specification what's the scope of the variables declared in the loop.
I don't know. But i have had no more issues with this since many years, so i hope the above is correct now with every compiler and i only exists within the scope inside the loop.