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. Global Objects Constructor

Global Objects Constructor

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 4 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.
  • T Offline
    T Offline
    Themis
    wrote on last edited by
    #1

    Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following: //-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ... //-- main.cpp int main(int argc, char* argv[]) { //code here ... } If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, Themis

    C S M 3 Replies Last reply
    0
    • T Themis

      Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following: //-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ... //-- main.cpp int main(int argc, char* argv[]) { //code here ... } If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, Themis

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      if you examine 'obj' once the program gets into main, can you tell if the ctor has been called ? i'm wondering if the ctor happens before the debugger can see... i do global vars in static libs all the time, and don't have any problems. Cleek | Image Toolkits | Thumbnail maker

      T 1 Reply Last reply
      0
      • C Chris Losinger

        if you examine 'obj' once the program gets into main, can you tell if the ctor has been called ? i'm wondering if the ctor happens before the debugger can see... i do global vars in static libs all the time, and don't have any problems. Cleek | Image Toolkits | Thumbnail maker

        T Offline
        T Offline
        Themis
        wrote on last edited by
        #3

        If you set a breakpoint at the constructor the debugger will see it. If I use the global var in my main program then the constructor is called. However what I want is not to use this object. I dont even want to know its existance out of my .lib, I just want to execute sth just before main begins. Themis

        1 Reply Last reply
        0
        • T Themis

          Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following: //-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ... //-- main.cpp int main(int argc, char* argv[]) { //code here ... } If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, Themis

          S Offline
          S Offline
          sunit5
          wrote on last edited by
          #4

          if u really want to know that global object ctor is call (which will definate be called when the object is instantiated).put this in the ctors cout<

          1 Reply Last reply
          0
          • T Themis

            Hi, I 'm experiencing the following problem with global objects. In a program with global objects at the beginning the constructors of these objects are executed and then the program enters in the main function. Everything its ok. Now I doing the following: //-- foo.cpp #include "foo.h" ///< include my header file MyClass obj; ///< Global object its constructor should run before main //-- MyClass member functions follow MyClass::MyClass(void) { ... } ... //-- main.cpp int main(int argc, char* argv[]) { //code here ... } If the following code is compiled as one project everything is fine. Now the trick is that I compile foo.cpp as a static library (foo.lib) to which my main program links. Running step by step with the debugger (VS .NET 7.1) I found out that main is called first. Actually placing a breakpoint in my object's constructor I noticed that the constructor is never executed. Why does that happens? If I compile foo.cpp as a dynamic library (dll) will I have the same problem? Thank you, Themis

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            If the MyClass ctor/dtor have no code, and no other code references obj, then the linker will discard the global object because it's not being used. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Wizard needs food, badly!

            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