The information in this article applies to:
SYMPTOMS
An extern declaration incorrectly causes an extra constructor call when the
extern declaration and the global declaration appear in the same
translation unit.
RESOLUTIONThere are many potential workarounds. The basic condition is that the extern declaration cannot appear after the global variable declaration in the same translation unit. One of the easiest ways to assure this condition is to place the offending global variable declaration(s) in a source file of their own that is included in the project, but not included in any other source files (please see the example code below). STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. MORE INFORMATIONSample Code
When you build and run the code above, you will see that MyClass::MyClass() is being called twice. One of the calls is generated by the global variable declaration in the file MyClass.h and the other by the extern declaration in the file main.cpp. One possible workaround is to remove the declaration of the global variable X from MyClass.h above and create a third file, "globals.cpp" that contains the declaration of the global variable X. By doing this, the declaration of the global variable resides in a different translation unit than the extern declaration(s).
When you build and run the program, it will now show that MyClass::MyClass() is only called once. Additional query words:
Keywords : kbcode kbtool kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600bug |
Last Reviewed: May 18, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |