Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

How to initialize a static structure?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 11 Oct 2007 00:34 GMT
Hi all,

I have a private static structure in a C++ class (it's a CRITICAL_SECTION
structure) that needs to be initialized by passing its address to a routine
(InitializeCriticalSection).  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
Brian Muth - 11 Oct 2007 01:01 GMT
> 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.
Carl Daniel [VC++ MVP] - 11 Oct 2007 02:49 GMT
> Hi all,
>
[quoted text clipped - 4 lines]
> initialize my static member variable prior to allowing any instances
> to be constructed?

struct CriticalSection : CRITICAL_SECTION
{
   public:
       CriticalSection()
       {
           ::InitializeCriticalSection(this);
       }

       ~CriticalSection()
       {
           ::DeleteCriticalSection(this);
       }
};

Now, just use CriticalSection instead of CTRITICAL_SECTION 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
Bob Altman - 12 Oct 2007 22:05 GMT
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

>> Hi all,
>>
[quoted text clipped - 25 lines]
>
> -cd
Carl Daniel [VC++ MVP] - 13 Oct 2007 23:58 GMT
> 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(int* p);

// Helper typedef
typedef init_t<int,some_init_fn> InitInt;

// 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
Bob Altman - 17 Oct 2007 18:13 GMT
Thanks Carl!

 - Bob

>> That's tricky... Instead of initializing the structure in my
>> (non-existent) static constructor, we're creating a self-initializing
[quoted text clipped - 45 lines]
>
> -cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.