Article ID: 152211
Article Last Modified on 7/5/2005
// This sample demonstrates the problem of the constructor getting
// called twice.
/* Compile options needed: cl test.cpp
*/
// TEST.CPP
// ========
#include "stdio.h"
class myClass {
public:
myClass() {printf("myClass Constructor called\n");};
void test() {printf("test() called\n");};
} object;
void main()
{
// Remove the next line for the first workaround:
extern myClass object;
object.test();
}
// sample output: (without extern line removed)
myClass Constructor called
myClass Constructor called
test() called
// This sample demonstrates the workaround that causes the program
// to only call the constructor once.
/* Compile options needed: cl Test2A.cpp Test2B.cpp
*/
// Test2.h
// ========
#include "stdio.h"
class myClass {
public:
myClass() {printf("myClass Constructor called\n");};
void test() {printf("test() called\n");};
};
// Test2A.cpp
// ==========
#include "Test2.h"
myClass object;
// Test2B.cpp
// ==========
#include "Test2.h"
void main()
{
extern myClass object;
object.test();
}
// sample output:
myClass Constructor called
test() called
Additional query words: 10.10 10.20
Keywords: kbbug kbfix kbnoupdate kbcpponly kbcompiler KB152211