BUG: extern Declaration Generates Extra Constructor CallID: Q167359
|
An extern declaration incorrectly causes an extra constructor call when the
extern declaration and the global declaration appear in the same
translation unit.
NOTE: Here, translation unit means the source file and any included files
considered as though they were all one file. Please see the example code
below.
There 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).
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
/* Compile Options Needed: /GX */
// File MyClass.h
#include <iostream>
#pragma once
struct MyClass {
MyClass()
{std::cout << "MyClass ctor Exectuing" << std::endl;}
};
MyClass X;
// End File MyClass.h
// File main.cpp
#include "myclass.h"
extern MyClass X;
int main()
{
return 0;
}
// End File main.cpp
/* Compile Options Needed: /GX */
// File MyClass.h
#include <iostream>
#pragma once
struct MyClass {
MyClass()
{std::cout << "MyClass ctor Exectuing" << std::endl;}
};
// End File MyClass.h
// File Globals.cpp
#include "MyClass.h"
MyClass X;
// End File Globals.cpp
// File main.cpp
#include "myclass.h"
extern MyClass X;
int main()
{
return 0;
}
// End File main.cpp
Additional query words:
Keywords : kbcode kbtool kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600bug
Version : winnt:4.0,4.1,4.2,5.0,6.0
Platform : winnt
Issue type : kbbug
Last Reviewed: May 17, 1999