Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

VC2008 compiler warning for variable shadowing?

This topic is 3633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does such a warning exist? If yes what is the code? I searched the MSDN library and web but couldn't find anything specific. Now manually going through the C4000 warnings... ...why I need this is: I went from VC6 to VC2008 and the for-loop variable scope is correct now. So I replaced (replace in files) all "for (i=" with "for (int i=" but didn't realise I could be bumping into variable shadowing problems that don't cause an error, like following example: int i=0; for (int i=0;i<10;i++) { if (someCondition) break; } do something with i (the one from the for-loop) Or any alternative solution? (I don't want to have to handle individually the more than 2000 errors caused by the now correct for-loop scope)

Share this post


Link to post
Share on other sites
Advertisement
You can't do that. As soon as you leave the scope of the for loop, the 'i' declared in it's initialiser doesn't exist anymore, and in all the other places you have similar problems, you'll always be using the variable that was declared most recently.

But if you're having large problems with this, you'll want to go over your code line by line and make sure it's all okay. You'll probably have to rename some of the variables. Also, let's ignore the compiler warnings for a moment and say that if you're looking at a block of code and can't immediately tell which variable that a particular identifier is representing, you really need to change it anyway.

Edit:: I also had to go through teething problems like this when moving from VS6 to VS2005, and let me tell you, it ended up being better in the long run.

Share this post


Link to post
Share on other sites
Well, it's not about not being able to tell what variable is doing what in a block of code, it's about the time to go through all 2000 occurences! And by the way I use the variable "i" only as a counter in for-loops, sometimes when breaking out of the loop the value is then used. I don't need to find a workaround, I just need the compiler to output an error or warning if a shadowing happens in a function. Looking at some of the warnings that can be activated, it seems the compiler keeps track of every single thing that happens, so why not also detecting when variables are shadowed inside the same routine!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!