Connecting Tech Pros Worldwide Help | Site Map

Initialize global variable before any other global variables

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 12:05 PM
jubelbrus
Guest
 
Posts: n/a
Default Initialize global variable before any other global variables

I need to initiate a global variable to a class before the
initialization of any other global variable.
The problem is that when I link the application with a dll, some or
all global variables in the dll is initialized before mine.

Is there a way to ensure that my global variable is initialized before
any other?

Take a simple class

class test {

}

#pragma init_seg(compiler)
test t;

According to msdn, "Objects in this group are constructed first".
That's the "compiler" group.

I'm using Microsoft Visual Studio.net 2005/VC++ on Windows XP


  #2  
Old July 18th, 2007, 12:35 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Initialize global variable before any other global variables

jubelbrus wrote:
Quote:
I need to initiate a global variable to a class before the
initialization of any other global variable.
The problem is that when I link the application with a dll, some or
all global variables in the dll is initialized before mine.
>
Is there a way to ensure that my global variable is initialized before
any other?
Well... Search the FAQ for "fiasco". Draw your own conclusions.
Quote:
Take a simple class
>
class test {
>
}
>
#pragma init_seg(compiler)
test t;
>
According to msdn, "Objects in this group are constructed first".
That's the "compiler" group.
There is no such thing in standard C++ language. It's something that
most likely unique to that compiler.
Quote:
I'm using Microsoft Visual Studio.net 2005/VC++ on Windows XP
Then you need to ask in the newsgroup dedicated to that compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #3  
Old July 19th, 2007, 02:35 PM
James Kanze
Guest
 
Posts: n/a
Default Re: Initialize global variable before any other global variables

On Jul 18, 1:59 pm, jubelbrus <thomas.heggel...@gmail.comwrote:
Quote:
I need to initiate a global variable to a class before the
initialization of any other global variable.
Why? Check out the singleton idiom. It can't guarantee this,
but it usually guarantees enough---that the variable is
initialized before it is used.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  #4  
Old July 19th, 2007, 10:25 PM
JohnQ
Guest
 
Posts: n/a
Default Re: Initialize global variable before any other global variables


"James Kanze" <james.kanze@gmail.comwrote in message
news:1184855301.343186.294920@z24g2000prh.googlegr oups.com...
On Jul 18, 1:59 pm, jubelbrus <thomas.heggel...@gmail.comwrote:
Quote:
I need to initiate a global variable to a class before the
initialization of any other global variable.
"Why? Check out the singleton idiom. It can't guarantee this,
but it usually guarantees enough---that the variable is
initialized before it is used."

The trickier issue is making sure it sticks around long enough after main
because chances are, that if it needs to be used by other globals before
main that it will be used after main also.

John

  #5  
Old July 20th, 2007, 01:15 PM
James Kanze
Guest
 
Posts: n/a
Default Re: Initialize global variable before any other global variables

On Jul 20, 12:19 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
Quote:
"James Kanze" <james.ka...@gmail.comwrote in message
Quote:
Quote:
news:1184855301.343186.294920@z24g2000prh.googlegr oups.com...
On Jul 18, 1:59 pm, jubelbrus <thomas.heggel...@gmail.comwrote:
Quote:
Quote:
Quote:
I need to initiate a global variable to a class before the
initialization of any other global variable.
Quote:
Quote:
Why? Check out the singleton idiom. It can't guarantee
this, but it usually guarantees enough---that the variable
is initialized before it is used.
Quote:
The trickier issue is making sure it sticks around long enough
after main because chances are, that if it needs to be used by
other globals before main that it will be used after main
also.
In practice, that's less often a problem; destructors generally
don't need nearly as much as constructors. And in the classical
Singleton idiom (in the GoF), the singleton object is never
destructed anyway. (My generic singleton offers the choice, but
defaults to never destructing, since that is the prefered
behavior 99% of the time.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.