On Thu, 20 Dec 2007 22:35:22 -0800 (PST), Sarath <CSar...@gmail.com>
wrote:
I've to write a single instance class. there are different methods to
control the single instance of a program
class CSingleton
{
public:
CSingleton& GetInstance(){ static CSingleton s; return s; }
private:
CSingleton(){}
~CSingleton(){}
}
The above code failed to compile in Visual C++ 6.0 but compiled in
Visual C++ 7.1 and Visual C++ Express 2008. CRT calling the destructor
of the class. So that Visual C++ 6.0 compilation error is correct
according to the concept.
I also tried in Dev C++. It was successful but didn't call the dtor of
the class. Which implementation is correct according to the standard.
I would avoid using this scheme because of several reasons:
First you don't have - as you have seen - control of the destruction
of the singleton. The CRT may or maynot call it before your last use
of the singleton has gone out of scope. The CRT doesn't know.
Second it is not thread safe, you are relying on that the compiler
generates a thread-safe implementation of the static initialization of
the variable however there is no guarantee that it is thread-safe.
hth/ajk- Hide quoted text -
- Show quoted text -
It's a single threaded application. No additional threads are there