By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

some thoughts about static class member type CX of class CX

P: n/a
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?

Aug 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Aug 21, 8:28 pm, ".rhavin grobert" <cl...@yahoo.dewrote:
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

Aug 21 '07 #2

P: n/a
..rhavin grobert wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.