Re: Mimicking Javas static class initializer in C++
On Oct 27, 11:37 pm, Andreas Wollschlaeger <meister.pos...@arcor.de>
wrote:
Lars Tetzlaff schrieb:
Andreas Wollschlaeger schrieb:
as the subject says, i'm a poor Java programmer trying to
transfer some of his wisdom into C++ world... here is what
im trying to do this evening:
[...]
Well, great, this was just what i have been looking for:
encapsulating the statics initialization in some inner class
and its default constructor - much more elegant and
"cplusplusish" than my previous attempt :-) Tx to you and the
other folks, added me some more insight to C++ this evening!
Just a bit of additional information. This isn't quite like
Java. C++ is statically linked, and the initialization code will
be executed before entering main (with all existing compilers,
anyway), where as Java uses lazy dynamic loading, and the
initialization code won't be executed until you load the class,
which won't happen until you use something in it. The C++ model
has one definite advantage: the initialization code will be
executed even if you never directly reference the class (e.g.
because it is a derived class, which registers its factory in
the static initializer---a frequent C++ idiom). It also has a
downside: the order of initialization isn't defined, so if one
static initializer depends on another, you may have problems.
(The usual work-around for this is to use the singleton idiom.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34