On Aug 21, 8:28 pm, ".rhavin grobert" <cl...@yahoo.de> wrote:
> i have (do try to have?) the following...
>
> [1] & [2] = breakpoints in debugger
>
> // ----------------------------------------------------------------
> // cx.h
> class CX {
> public:
> CX(CX* pcx = NULL);
> virtual ~CX();
>
> int iFoo;
> int fnFoo();
>
> private:
> static CX s_cx;
>
> };
>
> // ----------------------------------------------------------------
> // cx.cpp
>
> /*[1]*/ CX CX::s_cx(0);
>
> /*[2]*/ CX::CX(CX* pcx) {
> //do something...
>
> }
>
> //________________________________________
>
> i always thougt that the initialisation of the static class member at
> [1] will be called before any object is constructed [2], but in my
> implementation (the example is just an abstraction) there are several
> call of the ctor *before* the static member is initialized.
>
> the class in my implementation is something like this:
>
> it consists of a private std::vector of struct {void* p; int iID; /
> *some other data ....*/} and some fuctions for manipulating items
> (normally by iID). the whole class shall have a static class member of
> its own class to register all class objects, that is: the constructor
> calls a static function "register(this, iID);" and the static function
> "register" fills the static class vector.
>
> the problem is that the ctor will be called (4 times for 4 different
> vectors, breakpoint [2]) and the static class vector if filled with 4
> structs *and then* its initialisation is called (breakpoint [1]) and
> the vector is cleared().
>
> how do i force the initialisation of all static class mebers *before*
> any constructor is called?
Use static member function instead of static member object. Refer
http://www.parashift.com/c++-faq-lit...html#faq-10.15 for
details.
-N