Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Visual C++ static member initialization

Visual C++ static member initialization

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrawati
    wrote on last edited by
    #1

    Hi 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, yesterday I use that file on a different project (it's a directshow filter project) and it seems that instanceofB is never initialized there! I tried to debug it by making B's constructor to write to a file, but when I checked, the file was never written into, it's as if B's constructor's never called at all. I did that for B's default constructor too, but with the same result. I checked in all the project settings but I couldn't find out what causes this discrepancy. Could someone point me on what's the possible causes for this? Thanks!

    L C 2 Replies Last reply
    0
    • I Indrawati

      Hi 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, yesterday I use that file on a different project (it's a directshow filter project) and it seems that instanceofB is never initialized there! I tried to debug it by making B's constructor to write to a file, but when I checked, the file was never written into, it's as if B's constructor's never called at all. I did that for B's default constructor too, but with the same result. I checked in all the project settings but I couldn't find out what causes this discrepancy. Could someone point me on what's the possible causes for this? Thanks!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      what about putting breakpoints in B's constructor? Or could you maybe declare the instanceofB inside A's constructor?. Maybe the compiler did realized that instanceofB was never used and therefore no code was generated?

      1 Reply Last reply
      0
      • I Indrawati

        Hi 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, yesterday I use that file on a different project (it's a directshow filter project) and it seems that instanceofB is never initialized there! I tried to debug it by making B's constructor to write to a file, but when I checked, the file was never written into, it's as if B's constructor's never called at all. I did that for B's default constructor too, but with the same result. I checked in all the project settings but I couldn't find out what causes this discrepancy. Could someone point me on what's the possible causes for this? Thanks!

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #3

        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

        I 1 Reply Last reply
        0
        • C cmk

          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

          I Offline
          I Offline
          Indrawati
          wrote on last edited by
          #4

          Hi aString is a global constant defined in a separate header file. Actually, I defined it as follows: const char *const aString = "anything"; I *think* it should be no problem. Could you please elaborate on the second potential problem (about instanceofB being linked in), since I'm not very clear about it? Thanks!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups