Click here to Skip to main content

C / C++ / MFC

    RSS: RSS Feed

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page  Show 
  Refresh
GeneralRe: starting an application at startup, before logging in PinsussAnonymous2:24 4 Aug '04  
QuestionSend Messages? PinmemberZero_One_ADO19:10 3 Aug '04  
Answerno Pinmemberbikram singh19:44 3 Aug '04  
GeneralVisual C++ WinSNMP API: PinsussAlan Lewis17:00 3 Aug '04  
GeneralOptimization PinmemberSibilant15:54 3 Aug '04  
GeneralVisual C++ static member initialization PinmemberIndrawati15:25 3 Aug '04  
GeneralRe: Visual C++ static member initialization Pinmemberboggelibopp23:10 3 Aug '04  
GeneralRe: Visual C++ static member initialization Pinmembercmk1:51 4 Aug '04  
Indrawati wrote:
Let's say I have a class A. A has a static member whose type is another class, let's call it class B. Usually in my C++ application I declare this static member at the top of A's .cpp file as follows:
 
B A::instanceofB(aString)
 
where aString is of std::string type and is the argument for B's constructor.
 
That works OK and I got the result I wanted. However, ... it seems that instanceofB is never initialized there!

 
Where/how/when is aString initialized - i hope it's before instanceofB. If not then instanceofB is initialized (at best) with an empty string.
 
This alone is a potential problem. This is why you try to not to polute the global pool, especially with complex objects, especially with complex constructors.
You have little control over the order globals are initialized across modules.
 
Basically (if i remember right) globals within a module (c/cpp file) are initialized in the order they appear. Modules have their globals initialized in an effectively undefined order. That is, you don't know if a.cpp globals are initialized before or after b.cpp globals.
 
You can define some control over this using the #pragma init_seg(...).
#pragma init_seg(compiler) - initialized 1st
#pragma init_seg(lib) - initialized 2nd
#pragma init_seg(user) - initialized 3rd
#pragma init_seg(my_seg) - all initialized after user, but in no other defined order
 
The main limits with the above are :
- you should really never use compiler unless you know what you are doing
- you can only really use init_seg() once per module and it applies to all globals that follow
 
This may not be the problem.
 
Another thing to look at is if instanceofB is being linked in - with the little info provided i can't see why it wouldn't though.
 
Another thing is that the file i/o state may not be initialized by the time you try to initialize instanceofB. This would prevent B::B() from writing to a file. Try using OutputDebugString().

 
...cmk
 
Save the whales - collect the whole set
GeneralRe: Visual C++ static member initialization PinmemberIndrawati16:09 4 Aug '04  
QuestionHow to create a Point Custom Control PinmemberSSquared14:28 3 Aug '04  
GeneralMultiline toolbar button Pinmemberblase_3114:23 3 Aug '04  
Generaltotal number digit in string name. Pinmemberpnpfriend12:43 3 Aug '04  
GeneralRe: total number digit in string name. PinmemberBhaskar1:37 4 Aug '04  
GeneralRe: total number digit in string name. PinmemberIvan Cachicatari5:02 4 Aug '04  
GeneralRe: total number digit in string name. Pinmemberpnpfriend5:56 4 Aug '04  
GeneralLoading Bitmaps PinmemberDon1211:57 3 Aug '04  
GeneralOpen With... Dialog in Windows Explorer Pinmemberotrcomm11:16 3 Aug '04  
GeneralRe: Open With... Dialog in Windows Explorer Pinmemberpalbano11:33 3 Aug '04  
GeneralRe: Open With... Dialog in Windows Explorer Pinmemberotrcomm13:04 3 Aug '04  
GeneralRe: Open With... Dialog in Windows Explorer Pinmemberpalbano11:59 4 Aug '04  
GeneralRe: Open With... Dialog in Windows Explorer Pinmemberotrcomm17:15 4 Aug '04  
GeneralRe: Open With... Dialog in Windows Explorer Pinmemberpalbano17:33 4 Aug '04  
GeneralRe: Open With... Dialog in Windows Explorer Pinmemberotrcomm19:33 4 Aug '04  
Generallooking for MS (?) MFC sample program. PinmemberMaximilien11:05 3 Aug '04  
GeneralRe: looking for MS (?) MFC sample program. PinmemberJohan Rosengren11:43 3 Aug '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 14 Feb 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid