[Gc] C++ API enhancement proposal

skaller skaller at users.sourceforge.net
Sun Oct 14 10:20:37 PDT 2007


On Sun, 2007-10-14 at 11:35 -0400, MenTaLguY wrote:
> On Fri, 2007-05-11 at 08:48 +0200, Christophe Meessen wrote:
> > In C++ one can allocate global variable with a user defined class type.
> > The class type may thus have a user defined constructor in which he may
> > do many interrestings things including allocating blocks. The problem is
> > that these allocations may be done priror to calling GC_INIT() because
> > global variable instantiation is done before calling main.
> 
> Generally, in C++, instead of having a raw global object:
> 
>  Foo some_global(1, 2, 3);
> 
> ...the best practice is to do something like this, using a global
> accessor function instead:
> 
>  Foo &some_global() {
>    static Foo *ptr=NULL;
>    if (!ptr) {
>      ptr = new Foo(1, 2, 3);
>    }
>    return *ptr;
>  }
> 
> (In a multithreaded program, you must also add appropriate
> synchronization to the accessor.)

I beg to differ. The best practice is to use only global POD constants.


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


More information about the Gc mailing list