어떻게 든 :글로벌 객체 생성자가 호출 받고되지
static int num = 0;
class TestClass
{
public:
int a;
float b;
TestClass(float f)
{
a = 55;
b = f;
++num;
}
};
TestClass test(32);
int main(int argc,char *argv[])
{
std::cout<<"Int: "<<test.a<<std::endl;
std::cout<<"Float: "<<test.b<<std::endl;
std::cout<<"Constructor has been called "<<num<<" times"<<std::endl;
while(true);
return 0;
}
출력은 : 나는이 같은 생성자를 사용했습니다
Int: 0
Float: 0
Constructor has been called 0 times
이전과는 다른 프로젝트 였고 이런 문제가 없었습니다. 매개 변수없는 생성자를 시도하고 테스트 변수를 정적으로 만들지 않았으며 다른 효과가 없었습니다. 누가이 일이 벌어지고 있는지 나에게 깨달을 수 있습니까?
차이점이있을 경우를 대비하여 Visual Studio 2010을 사용하고 있습니다.
관심있는 사람이라면 [ideone] (http://ideone.com/3pnvDc)에서 잘 작동합니다. – Dukeling
코드가 정확한 결과를 제공합니다. –
정확한 코드인가요? 특히 동일한 파일에 전역'TestClass test'와'int num' 카운터가 있습니까? –