473,769 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(t his, 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 1901
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(t his, 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
2041
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 constness of static members. Is there a way to do that? Also, is there a way for a static member function to guarantee constness of static members? TIA Regards,
7
2148
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 y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array class see below
6
2472
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 type of its own class. Non-static data members can not be declared as type of its own class (excluding pointers and references).
7
2522
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 mode, I suddenly get a ton of these errors: cs(1189): 'class.form.checkedListBox1' denotes a 'field' where a 'class' was expected I was not getting any errors until I made a couple of changes within VS.NET. So I'm trying to understand why...
39
2531
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 Bar(int i) const; } (I said that since Bar is a static method, it can only access
1
2299
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 the data member is the same for every instance of that class? I would expect so, but I noticed something that makes me doubt a little.
8
2889
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
7871
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
5824
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 Element_common<L2, Side<2,2 { public:
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.