Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > some thoughts about static class member type CX of class CX

Reply
Thread Tools

some thoughts about static class member type CX of class CX

 
 
.rhavin grobert
Guest
Posts: n/a
 
      08-21-2007
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?

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      08-21-2007
On Aug 21, 8:28 pm, ".rhavin grobert" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-21-2007
..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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM



Advertisments