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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to implement Global Pointers ?

How to implement Global Pointers ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancetutorial
6 Posts 5 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.
  • O Offline
    O Offline
    oRion
    wrote on last edited by
    #1

    I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:

    C C T 3 Replies Last reply
    0
    • O oRion

      I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      There is no class here, only a namespace. You just said they were globals, didn't you ? You can call new in the .cpp file. You could also assign to NULL, and have your classes call new if NULL. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

      A 1 Reply Last reply
      0
      • O oRion

        I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:

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

        do your "new" in your CWinApp's InitInstance. -c


        WWJD? JWRTFM.
           found on /.

        image effects!

        1 Reply Last reply
        0
        • C Christian Graus

          There is no class here, only a namespace. You just said they were globals, didn't you ? You can call new in the .cpp file. You could also assign to NULL, and have your classes call new if NULL. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Thanks for the reply. I will try it out. Thanks a million! ;)

          1 Reply Last reply
          0
          • O oRion

            I have implemented global variables with the method that is going round this message board. That is to write a header file with all my global variables and a corresponding .cpp file to call it. global.h namespace myspace{ extern int myvar,*myptr; } global.cpp int myspace::myvar,*myspace::myptr; This work perfectly;), but I need my pointer to be initialize with new() statement for the first use. I would like to put it in the global.cpp file so that this is done automatically when this global file is being used. How do I do that? Could I do it using Class ?? If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? :confused:

            T Offline
            T Offline
            Todd Smith
            wrote on last edited by
            #5

            Declare a class in global.cpp and make a single instance of that class

            class CGlobal
            {
            private:
            CGlobal()
            {
            myptr = new int[32];
            }

            ~CGlobal()
            {
                delete \[\] myptr;
            }
            

            };

            static CGlobal globalInit;

            oRion wrote: If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? Yes. Just don't derive from the class. That can be prevented by making the constructor private. The other option is to make a GlobalInit() and GlobalCleanup() function which does your allocations and call it at the beginning of your program. Todd Smith

            O 1 Reply Last reply
            0
            • T Todd Smith

              Declare a class in global.cpp and make a single instance of that class

              class CGlobal
              {
              private:
              CGlobal()
              {
              myptr = new int[32];
              }

              ~CGlobal()
              {
                  delete \[\] myptr;
              }
              

              };

              static CGlobal globalInit;

              oRion wrote: If the new() statement is implemented in the constructor, would this means every class derive from this Base Class, would call new() statement in the constructor? This means redudant memory allocation is done here? Yes. Just don't derive from the class. That can be prevented by making the constructor private. The other option is to make a GlobalInit() and GlobalCleanup() function which does your allocations and call it at the beginning of your program. Todd Smith

              O Offline
              O Offline
              oRion
              wrote on last edited by
              #6

              Todd Smith wrote: Declare a class in global.cpp and make a single instance of that class class CGlobal{private: CGlobal() { myptr = new int[32]; } ~CGlobal() { delete [] myptr; }};static CGlobal globalInit; Thanks for the reply. I understand the portion on using private for constructor. Could u help tell me more of the statement on static CGlobal globalInit; ? Where should this be declared in? Why is there a need to declare it as a static ? :confused:

              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