Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!att!drutx!kvt From: kvt@drutx.ATT.COM (TranKV) Newsgroups: comp.lang.c++ Subject: a question about design simplicity Summary: static objs vs. global objs Keywords: static, global Message-ID: <4769@drutx.ATT.COM> Date: 2 Jan 90 22:06:47 GMT Reply-To: kvt@drutx.ATT.COM (TranKV) Organization: AT&T, Denver, CO Lines: 42 References: 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