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. HHEEELLLPPPP: global integer!

HHEEELLLPPPP: global integer!

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 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.
  • S Offline
    S Offline
    Sunnygirl
    wrote on last edited by
    #1

    hello @all, how can i make an integer to a global variable? i must use it, in different classes (CDialogA, CDialogB, ...)! thank you very much sunny

    J B J 3 Replies Last reply
    0
    • S Sunnygirl

      hello @all, how can i make an integer to a global variable? i must use it, in different classes (CDialogA, CDialogB, ...)! thank you very much sunny

      J Offline
      J Offline
      Joao Paulo Figueira
      wrote on last edited by
      #2

      Declare your variable in any .cpp file. Define it in a .h file as extern. Include this file whenever you need the variable.

      S 1 Reply Last reply
      0
      • S Sunnygirl

        hello @all, how can i make an integer to a global variable? i must use it, in different classes (CDialogA, CDialogB, ...)! thank you very much sunny

        B Offline
        B Offline
        berndg
        wrote on last edited by
        #3

        How about this: (a) /// declaration int iMyGlobalInteger = ; (b) /// reference: extern int iMyGlobalInteger; (c) Reconsider all the above and change your plan. Global variables are evil. Whenever you have a global variable, you have a design flaw. (d) Reconsider all the above. Whenever you don't know how to implement a global variable, you have a severe C language knowledge problem. This is very basic knowledge, if you don't mind me being frank. Stop whatever programming you're at right now. Stop it immediately. Go out, buy and read a good book on C/C++, then return to the design of the tool (see (c), above). Bernd :suss:

        S 1 Reply Last reply
        0
        • S Sunnygirl

          hello @all, how can i make an integer to a global variable? i must use it, in different classes (CDialogA, CDialogB, ...)! thank you very much sunny

          J Offline
          J Offline
          Joan M
          wrote on last edited by
          #4

          You can do it in several ways: 1. Declare it in a header file included in all the classes you need. 2. Declare it in a cpp file and outside the class scope, and then try to reach it with the "extern" keyword. 3. My favourite one: In the Application object (typically "TheApp"), declare it inside the class that defines the application, and then reach the application object... (you can be almost sure that you'll be able to reach the header file that contains the application from every header or implementation file in your project. MoreOver I would declare the variable as private and give the SET/GET interface functions in order to access it (in order to be more standard and to be able to check what is happening always and easily)... Sample:

          header file of the application
          class CTCCApp : public CWinApp
          {
          public:
          CTCCApp();
          ...

          private:
          int theIntegerVarToBeAccessed;

          public:
          int GetTheIntegerValue();
          void SetTheIntegerValue(int iValue);

          implementation file of the application
          /*---------------------------------------------
          COMMENTS FIRST

          1. Initialize your variable at the application's constructor or in initinstance...
          2. You'll be able to see that somewhere near top the implementation file there's a line like this one:
            CTCCApp TCCApp; (You should substitute "TCC" for your text string) This is your app....
            ---------------------------------------------*/

          // Now the functions that you use to access the number...
          // SETTING THE VALUE:
          void CTCCApp::SetTheIntegerValue(int iValue)
          {
          // Process the value and make whatever you want with it before setting it to your integer variable...
          theIntegerVarToBeAccessed = iValue;
          }

          // GETTING THE VALUE:
          int CTCCApp::GetIntegerValue()
          {
          return theIntegerVarToBeAccessed;
          }

          OK, now it's time to get access to those functions from everywhere you need...

          Where you need to access that integer (let's say WHERE.cpp)
          /*---------------------------------------------
          COMMENTS FIRST
          Be sure that you have a #include "TCCApp.h"; in your header or in your implementation file.
          ---------------------------------------------*/
          //Inside the function where you want to access that:
          extern CTCCApp TCCApp;
          // Now you can get:
          TCCApp.GetIntegerValue();
          TCCApp.SetIntegerValue(10);

          I think that this is the best method because you can "share" a lot of datatypes (even pointers to...) and you can make it in a safe and sorted way... Hope this helps...

          https://www.robotecnik.com freelance robots, PLC and CNC programmer.

          1 Reply Last reply
          0
          • B berndg

            How about this: (a) /// declaration int iMyGlobalInteger = ; (b) /// reference: extern int iMyGlobalInteger; (c) Reconsider all the above and change your plan. Global variables are evil. Whenever you have a global variable, you have a design flaw. (d) Reconsider all the above. Whenever you don't know how to implement a global variable, you have a severe C language knowledge problem. This is very basic knowledge, if you don't mind me being frank. Stop whatever programming you're at right now. Stop it immediately. Go out, buy and read a good book on C/C++, then return to the design of the tool (see (c), above). Bernd :suss:

            S Offline
            S Offline
            Sunnygirl
            wrote on last edited by
            #5

            thanks for reply. i make it in the same way (a) and (b). CDialogA: int global_int; ... void CDialogA::OnOk() { ... global_int=5; ... } CDialogB: extern int global_int; ... void CDialogB::OnQuestion() { ... int number=global_int; ... } but the number has not the same "content" as global_int in CDialogA. why? that´s why i asked. can you help me? sunny

            B 1 Reply Last reply
            0
            • J Joao Paulo Figueira

              Declare your variable in any .cpp file. Define it in a .h file as extern. Include this file whenever you need the variable.

              S Offline
              S Offline
              Sunnygirl
              wrote on last edited by
              #6

              thanks for reply. i make this: CDialogA: int global_int; ... void CDialogA::OnOk() { ... global_int=5; ... } CDialogB: extern int global_int; ... void CDialogB::OnQuestion() { ... int number=global_int; ... } but the number has not the same "content" as global_int in CDialogA. why? can you help me? sunny

              J 1 Reply Last reply
              0
              • S Sunnygirl

                thanks for reply. i make this: CDialogA: int global_int; ... void CDialogA::OnOk() { ... global_int=5; ... } CDialogB: extern int global_int; ... void CDialogB::OnQuestion() { ... int number=global_int; ... } but the number has not the same "content" as global_int in CDialogA. why? can you help me? sunny

                J Offline
                J Offline
                Joao Paulo Figueira
                wrote on last edited by
                #7

                What is the order of execution? CDialogA::OnOK() before CDialogB::OnQuestion()? To sort it out, declare the variable and assign it a dummy value (may be zero), then check if number is zero or five.

                S 1 Reply Last reply
                0
                • S Sunnygirl

                  thanks for reply. i make it in the same way (a) and (b). CDialogA: int global_int; ... void CDialogA::OnOk() { ... global_int=5; ... } CDialogB: extern int global_int; ... void CDialogB::OnQuestion() { ... int number=global_int; ... } but the number has not the same "content" as global_int in CDialogA. why? that´s why i asked. can you help me? sunny

                  B Offline
                  B Offline
                  berndg
                  wrote on last edited by
                  #8

                  Your code snippet doesn't indicate an error; I assume this is because you've reduced it too far for display in this thread. Alternatively, you might be looking into the data at the wrong times? After all, CDialogA would only set the variable to 5 in its OnOk() handler; your snippet doesn't indicate when CDialogB::OnQuestion() occurs with relation to CDialogA::OnOk(). I can only repeat (c): global variables are evil and, with very rare exceptions, a good indicator for poor design. Class A needs to communicate with class B? Send a message from A to B. Any class needs to communicate with any other class? Implement a controller class as a central communications hub (see: singleton design pattern). Any class needs to communicate with anything else in a haywired fashion in a way a singleton controller cannot resolve with elegance? Re-visit your design. Haywire comms is a bad sign, and a good indicator for poor design. Bernd

                  1 Reply Last reply
                  0
                  • J Joao Paulo Figueira

                    What is the order of execution? CDialogA::OnOK() before CDialogB::OnQuestion()? To sort it out, declare the variable and assign it a dummy value (may be zero), then check if number is zero or five.

                    S Offline
                    S Offline
                    Sunnygirl
                    wrote on last edited by
                    #9

                    oooohhhhhhhhh.....i know what´s wrong!!! thank you very very much.... João Paulo Figueira wrote: What is the order of execution? this was the point. i created a dialog at the beginning of my program and hide it and later i show it (inside this dialog was the problem). And so the order of execution was wrong!!!!! thank you very very much. :rose::rose::rose: sunny

                    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