kvt@drutx.ATT.COM (TranKV) (01/03/90)
I have a design question about the use of global
objs vs. static objs. Let me first describe a test problem:
Let's say I want to create a utility library. This library needs
a buffer that must be NEWed (cannot be on stack) once and only
once for all members of the library. I have two ways to do
this:
1. declare a global object whose constructor NEWs my buffer so
that the buffer is created before I hit 'main'. One obvious
problem is my internal object is known to the world (a NO-NO??).
2. To remove that problem, I can define a class init:
class init {
public:
init () {
if (!allocated) {
// NEW my buffer
}
++allocated;
}
~init () {
if (--allocated == 0) {
// delete my buffer
}
}
private:
static short allocated;
};
and then allocate static objects of class init in each source
file (through my library's header) to insure the allocation of my
buffer.
Solution (2) is much complicated but looks more 'object oriented' (|-).
Now, can you netters give me your opinions why one is better than the
other. Hope we'll have a good discussion.
Kim Tran
Bell Labs