473,657 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to initialize a static structure?

Hi all,

I have a private static structure in a C++ class (it's a CRITICAL_SECTIO N
structure) that needs to be initialized by passing its address to a routine
(InitializeCrit icalSection). Since C++ apparently doesn't allow a static
constructor routine, how do I initialize my static member variable prior to
allowing any instances to be constructed?

TIA - Bob
Oct 10 '07 #1
5 11113
Since C++ apparently doesn't allow a static
constructor routine, how do I initialize my static member variable prior to
allowing any instances to be constructed?
A static member variable can certainly have a constructor, and this will be called automatically when the DLL is loaded.
Oct 11 '07 #2
Bob Altman wrote:
Hi all,

I have a private static structure in a C++ class (it's a
CRITICAL_SECTIO N structure) that needs to be initialized by passing
its address to a routine (InitializeCrit icalSection). Since C++
apparently doesn't allow a static constructor routine, how do I
initialize my static member variable prior to allowing any instances
to be constructed?
struct CriticalSection : CRITICAL_SECTIO N
{
public:
CriticalSection ()
{
::InitializeCri ticalSection(th is);
}

~CriticalSectio n()
{
::DeleteCritica lSection(this);
}
};

Now, just use CriticalSection instead of CTRITICAL_SECTI ON when declaring
your variables. There are a million ways to twist this - this is just one
example of how you can get static constructor behavior for your critical
section variables.

-cd
Oct 11 '07 #3
That's tricky... Instead of initializing the structure in my (non-existent)
static constructor, we're creating a self-initializing structure. I like
it! Thanks!

So, as an academic question: Suppose I have an int variable that needs to
be initialized by passing its address to some routine. Is there some tricky
way to create a self-initializing static int variable?

- Bob

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Bob Altman wrote:
>Hi all,

I have a private static structure in a C++ class (it's a
CRITICAL_SECTI ON structure) that needs to be initialized by passing
its address to a routine (InitializeCrit icalSection). Since C++
apparently doesn't allow a static constructor routine, how do I
initialize my static member variable prior to allowing any instances
to be constructed?

struct CriticalSection : CRITICAL_SECTIO N
{
public:
CriticalSection ()
{
::InitializeCri ticalSection(th is);
}

~CriticalSectio n()
{
::DeleteCritica lSection(this);
}
};

Now, just use CriticalSection instead of CTRITICAL_SECTI ON when declaring
your variables. There are a million ways to twist this - this is just one
example of how you can get static constructor behavior for your critical
section variables.

-cd


Oct 12 '07 #4
Bob Altman wrote:
That's tricky... Instead of initializing the structure in my
(non-existent) static constructor, we're creating a self-initializing
structure. I like it! Thanks!
That's the C++ way. The C# static constructor is a crutch to smooth over
one of the warts left by not having deterministic construction and
destruction - not to mention initialized static variables.
>
So, as an academic question: Suppose I have an int variable that
needs to be initialized by passing its address to some routine. Is
there some tricky way to create a self-initializing static int
variable?
Sure - just make a class to do it.

template <typename T, void (*fn)(T*)struct init_t
{
init_t()
{
(*fn)(m_t);
}

operator T&
{
return m_t;
}

private:
T m_t;
};
// An initialization function
extern void some_init_fn(in t* p);

// Helper typedef
typedef init_t<int,some _init_fnInitInt ;

// An int that's initialized by passing it's address to that function
InitInt my_initialized_ int;

Again, one of a hundred different variations on a theme. If you truly want
the int to be an independent variable, then remove the private member and
pass it as a constructor parameter instead. Then you have to declared two
variables - one int and one init_t<int,void (*)(int*)>.

Boost (and various other template libraries) have pre-made portable,
reusable solutions to these problems and many more.

-cd

Oct 13 '07 #5
Thanks Carl!

- Bob

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:uO******** ******@TK2MSFTN GP04.phx.gbl...
Bob Altman wrote:
>That's tricky... Instead of initializing the structure in my
(non-existent) static constructor, we're creating a self-initializing
structure. I like it! Thanks!

That's the C++ way. The C# static constructor is a crutch to smooth over
one of the warts left by not having deterministic construction and
destruction - not to mention initialized static variables.
>>
So, as an academic question: Suppose I have an int variable that
needs to be initialized by passing its address to some routine. Is
there some tricky way to create a self-initializing static int
variable?

Sure - just make a class to do it.

template <typename T, void (*fn)(T*)struct init_t
{
init_t()
{
(*fn)(m_t);
}

operator T&
{
return m_t;
}

private:
T m_t;
};
// An initialization function
extern void some_init_fn(in t* p);

// Helper typedef
typedef init_t<int,some _init_fnInitInt ;

// An int that's initialized by passing it's address to that function
InitInt my_initialized_ int;

Again, one of a hundred different variations on a theme. If you truly
want the int to be an independent variable, then remove the private member
and pass it as a constructor parameter instead. Then you have to declared
two variables - one int and one init_t<int,void (*)(int*)>.

Boost (and various other template libraries) have pre-made portable,
reusable solutions to these problems and many more.

-cd

Oct 17 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3516
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum { lengthSV=16 }; // Length of SomeVector vector<double> SomeVector;
4
3013
by: Timothy Madden | last post by:
Hello everybody ! I have a function in a dll that will write bytes of data in a log file. The function has a local static FILE pointer like this: void VMLogPacket(BYTE *pData, size_t nSize) { static FILE *dbg = fopen("pakets.log", "wbc"); if (dbg)
7
46428
by: Andi.Martin | last post by:
Hi, how is it possible, to only initialize parts of a structure. Example: typedef struct{ int a, b; ... (huge lot of members); double x,y;
6
54975
by: Ramprasad A Padmanabhan | last post by:
I have a simple structure defined like this struct userid { char uid; int insize; int outsize; }; typedef struct userid user;
18
8637
by: vib | last post by:
Hi there, By chance, I came to learn that it is bad programming practice to initialize global variables at outside of programs. Is it that bad? In order to fullfil this, I had to declare them with const, but thereafter they are no longer variables. Just couldn't see the benefits of this practice. Any comments or advices are welcome. Thanks vib
1
1046
by: Bumbala | last post by:
Hello, Is it possible to initialize non-static members inside a value class (structure) ? Thanks.
4
8356
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I should initialize them in advance. However, the following code, in which I first produce two classses and then try to assign a reference of the first class to a static data member of the second class doesn't work. It gives the following compiler error:
4
1974
by: Bram Kuijper | last post by:
Okay, second try (since my posting on 4/27), to find a proper way to initialize a static reference member to an object. I try to initialize a static reference inside the class ga, referencing to an instance of the class bla. According to a previous posting of Zeppe on 4/27... Doing that, this is my code: class bla
3
2150
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For example, suppose MyStruct has 3 int members. I've tried something like this: class Test {
0
8310
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
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7333
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
6167
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
4158
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
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.