|
From: | Henri Manson |
Subject: | Classes with only one instance |
Date: | Tue, 5 Nov 2002 02:54:04 +0100 |
I have the following proposal to change
classes with only one instance e.g. ScreenManager as follows (an
example):
class
InstanceClass
{ public: int x; int y; static InstanceClass
instance;
InstanceClass() : x(3), y(4) {
}
~InstanceClass() { printf("cleaning up\n"); } }; InstanceClass
InstanceClass::instance;
int main(int argc, char* argv[]) { printf("%d %d \n", InstanceClass::instance.x, InstanceClass::instance.y); return 0; } The advantages are:
- No more need to implement init/deinit functions:
the object is automatically destroyed at program exit
- A direct object -> (slightly) better
performance
I'm very curious what your opinion
is.
This compiles under gcc and VC++
|
[Prev in Thread] | Current Thread | [Next in Thread] |