some thoughts about static class member type CX of class CX

R

.rhavin grobert

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?
 
N

Neelesh Bodas

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-lite/ctors.html#faq-10.15 for
details.

-N
 
V

Victor Bazarov

..rhavin grobert said:
[..]
how do i force the initialisation of all static class mebers *before*
any constructor is called?

This is (a) in the FAQ, (b) has recently been discussed again. So,
see the FAQ, search the archives (using Google Groups, for example).
Essentially, your question is "how do I call a constructor of CX
before any call to a constructor of CX". Rebuttal?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top