Article View: gnu.g++.help
Article #319initialization of objects within a library
From: Werner Engeln
Date: Thu, 22 Nov 1990 12:34
Date: Thu, 22 Nov 1990 12:34
99 lines
2890 bytes
2890 bytes
To ensure that global objects of a library is initialized befor its first
use, I implemented a counter to maintain the number of translation units
using it (see "the annotated C++ reference manual section 3.4, Ellis &
Stroustrup).[example below]
To my confusion, my library objects are constructed (and destructed) twice !
How can I reduce constructor calls to only ONE for each global object ??
mail, and I will post a summary
engeln@danetis.uucp
------------------------------- cut here -- nifty_library.h -----------
// nifty_library.h
#ifndef nifty_h
#define nifty_h
class foo {
public :
foo (const char *);
~foo ();
void print ();
private :
const char * name;
};
class nifty_counter {
public :
nifty_counter ();
~nifty_counter ();
private :
static count;
};
#ifndef nifty_cc
// global objects
extern foo aFoo;
// counter of translation units
static nifty_counter nifty;
#endif nifty_cc
#endif nifty_h
------------------------------- cut here -- nifty_library.cc ----------
#define nifty_cc
extern "C" void printf (const char *, ...);
// declare global objects to resolve external references
foo aFoo ("aFoo by library startup");
// memberfunctions
foo::foo (const char * c)
: name (c)
{
printf ("constructing %s\n", name);
}
foo::~foo ()
{
printf ("destructing %s\n", name);
}
void foo::print ()
{
printf ("foo named %s\n", name);
}
// counter implementation
nifty_counter::nifty_counter ()
{
if (count++ == 0) {
// initialize global objects
aFoo.foo ("aFoo by nifty_counter");
}
}
nifty_counter::~nifty_counter ()
{
if (--count == 0) {
// clean up global objects
aFoo.foo::~foo ();
}
}
------------------------------- cut here -- nifty_user.cc -------------
#include "nifty.h"
void main ()
{
aFoo.print ();
}
------------------------------- cut here -- output --------------------
constructing aFoo by nifty_counter
constructing aFoo by library startup
foo named aFoo by library startup
destructing aFoo by library startup
destructing aFoo by library startup
--
Werner Engeln Phone: +49-711/736051 FAX: +49-711/736054
Danet-IS GmbH UUCP: engeln@danetis.uucp
Waldburgstrasse 17 - 19 BITNET: engeln%danetis.uucp@unido.bitnet
D-7000 Stuttgart 80 X.400: C=DE;A=DBP;P=DANET;O=IS;S=Engeln
Technical Details
Message-ID:
<3306@danetis.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!unido!danetis!engelnShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!unido!danetis!engeln From: engeln@danetis.UUCP (Werner Engeln) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: initialization of objects within a library Keywords: c++,library,initialization Message-ID: <3306@danetis.UUCP> Date: 22 Nov 90 12:34:32 GMT Sender: news@danetis.UUCP Reply-To: engeln@danetis.UUCP () Organization: Danet-IS GmbH, Stuttgart, West Germany Lines: 99