BUG: The default behavior of the for statement destroys the object incorrectly (823841)
The information in this article applies to:
- Microsoft Visual C++ .NET (2002)
SYMPTOMSWhen you declare an object in the init-expression element of the for loop in Microsoft Visual C++ .NET 2002, and the object is used
outside the scope of the for statement, you may receive unwanted results. Note In Microsoft Visual C++ .NET 2003, you may receive the following
compiler error message: error C2065: <variable
name> : undeclared identifier CAUSE In Visual C++ .NET 2002, when the object is created in the init-expression element of the for statement, the object is destroyed as the object goes outside the
scope of the for statement. This behavior occurs even though the object is
accessible outside the scope of the for statement. RESOLUTION When you migrate an existing Visual C++ 6.0 project to
Visual C++ .NET 2002, always compile the migrated project in Visual C++ .NET
2002 with the /Zc:forScope compiler option. When you use the the /Zc:forScope compiler option, you may receive a compiler error message while
compiling the project. For each object that you receive a compiler error for,
declare the object before the for statement. The following is the modified code:
MyClass mc = 10;
for(;mc;--mc)
{
}
printf("Value of mc.m_i = %d\n",mc.m_i);
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed at the beginning of this article.
REFERENCESFor additional information about the Microsoft extensions
for C++ language, visit the following Microsoft Developer Network (MSDN) Web
site: For additional information about the C++ Compiler, Linker, and C++
Language, visit the following MSDN Web site: For additional information about the for statement, visit the following MSDN Web site:
Modification Type: | Minor | Last Reviewed: | 12/30/2005 |
---|
Keywords: | kberrmsg kbCompiler kbconvert kbbug KB823841 kbAudDeveloper |
---|
|