I expected that GetStudent() function returns unique instance of Student. As I expected, that function returns unique instance of Student Class. I double checked the memory address returned by GetStudent() function. But very strange thing was that Student's constructor called every time when I called GetSutent() function. The code like below. The programming enviroment was VC6.0 & MFC project.
//someApp.h
Student& GetStudent();
//someApp.cpp
Student& GetStudent()
{
static Student _student;
return _student;
}
//client1Class.cpp
#include "someApp.h"
void CCliend1Class::DoSomething()
{
GetStudent().DoSomething();
}
//client2Class.cpp
#include "someApp.h"
void CClient2Class::DoSomething()
{
GetStudent().DoSomething();
}
Studentsomewhere inDoSomething()?Studentis being called each timeGetStudent()is called, there's some important detail missing from the code you posted (even if you're using a compiler as old as VC6).