473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

some thoughts about static class member type CX of class CX

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
2 1878
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
7
by: Chris Clement | last post by:
I have been handed a project that someone else started and most of it was developed in the VS.NET design mode. For whatever reasons, when I try to make changes to the controls in VS.NET design...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
1
by: Frederiek | last post by:
Hi, When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. Does that mean that the address of...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.