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. Making an Object global

Making an Object global

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++databasediscussion
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.
  • V Offline
    V Offline
    viperlogic
    wrote on last edited by
    #1

    i am using VC++6 and MFC and using dialogs. I have a button that when clicked starts a method which creates an instance of a class, this object is then used to connect to the database and also does querys. how can i make this object seen by other methods and dialogs? can this object be created globally? or what is best practice?

    B S T 4 Replies Last reply
    0
    • V viperlogic

      i am using VC++6 and MFC and using dialogs. I have a button that when clicked starts a method which creates an instance of a class, this object is then used to connect to the database and also does querys. how can i make this object seen by other methods and dialogs? can this object be created globally? or what is best practice?

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #2

      Its best practice to have as little global objects as possible. There is at least on global object theApp (the application) which can be accessed through the function AfxGetApp() which returns a pointer to the application object. You can make you database access class a member of the application class. Then you can access this class through the Application pointer from anywhere in your application. codito ergo sum

      V 1 Reply Last reply
      0
      • B BadKarma

        Its best practice to have as little global objects as possible. There is at least on global object theApp (the application) which can be accessed through the function AfxGetApp() which returns a pointer to the application object. You can make you database access class a member of the application class. Then you can access this class through the Application pointer from anywhere in your application. codito ergo sum

        V Offline
        V Offline
        viperlogic
        wrote on last edited by
        #3

        many thanks for your repply could you further explain this with a code example please?? regards

        1 Reply Last reply
        0
        • V viperlogic

          i am using VC++6 and MFC and using dialogs. I have a button that when clicked starts a method which creates an instance of a class, this object is then used to connect to the database and also does querys. how can i make this object seen by other methods and dialogs? can this object be created globally? or what is best practice?

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          Too many globals are indeed bad, but that doesn't mean any use of globals is bad. Assuming you want a global variable here's one way to do it in C++:

          // In a .h file.
          extern int *g_pIntPointer;
          
          // In a .c/.cpp file.
          int *g_pIntPointer = NULL;
          

          Now in any file where you add a #include "TheDotHFile.h" your can access you global. There are other choices like putting the variable in a class and making it static. If you explain you situation more thoroughly I'm sure you can spark off a debate about the pros and cons of the various techniques. Steve

          1 Reply Last reply
          0
          • V viperlogic

            i am using VC++6 and MFC and using dialogs. I have a button that when clicked starts a method which creates an instance of a class, this object is then used to connect to the database and also does querys. how can i make this object seen by other methods and dialogs? can this object be created globally? or what is best practice?

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            what about putting your object/pointer in stdafx.h file.. which is included in almost easch file in mfc application.

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV

            1 Reply Last reply
            0
            • V viperlogic

              i am using VC++6 and MFC and using dialogs. I have a button that when clicked starts a method which creates an instance of a class, this object is then used to connect to the database and also does querys. how can i make this object seen by other methods and dialogs? can this object be created globally? or what is best practice?

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              what about putting your object/pointer in stdafx.h file.. which is included in almost easch file in mfc application... i.e. give a dummy declaration of variable with extern keyword there and declare it in the place you actually using it... i generally use Singleton class for this ... as it gurantee me a single object acceciable through out the project

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              cheers, Alok Gupta VC Forum Q&A :- I/ IV

              V 1 Reply Last reply
              0
              • T ThatsAlok

                what about putting your object/pointer in stdafx.h file.. which is included in almost easch file in mfc application... i.e. give a dummy declaration of variable with extern keyword there and declare it in the place you actually using it... i generally use Singleton class for this ... as it gurantee me a single object acceciable through out the project

                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                cheers, Alok Gupta VC Forum Q&A :- I/ IV

                V Offline
                V Offline
                viperlogic
                wrote on last edited by
                #7

                im getting a bit confused now! to further exlpain. i have a basic MFC dialog app that was created using the wizard. i want to connect to a database and execute querys, i am using a wrapper for this (mysqlwrapp). to make an object of the dbconnection class i use the following in a method that is invoked by a click of a button. CSQLConnection m_conn; //create object m_conn.Open(_T("localhost"), _T("salaries"), _T("root"), _T("root")); //connect then in another method i want to execute the following, but it cant see the m_conn object. this new object allows querys to database CSQLResult res(m_conn); res.Query(_T("SELECT Name FROM house")); to create these objects the "MySQLWrapp.h" is included in the dialog where the objects are created and used. so what is the best way to make the m_conn object global. could you please give example code on where and how as most of this is new to me. thanks

                S 1 Reply Last reply
                0
                • V viperlogic

                  im getting a bit confused now! to further exlpain. i have a basic MFC dialog app that was created using the wizard. i want to connect to a database and execute querys, i am using a wrapper for this (mysqlwrapp). to make an object of the dbconnection class i use the following in a method that is invoked by a click of a button. CSQLConnection m_conn; //create object m_conn.Open(_T("localhost"), _T("salaries"), _T("root"), _T("root")); //connect then in another method i want to execute the following, but it cant see the m_conn object. this new object allows querys to database CSQLResult res(m_conn); res.Query(_T("SELECT Name FROM house")); to create these objects the "MySQLWrapp.h" is included in the dialog where the objects are created and used. so what is the best way to make the m_conn object global. could you please give example code on where and how as most of this is new to me. thanks

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  The "m_" wart is normally used to denote a class member variable and not a local variable as you’re using it. Follow these steps: 1. Remove the CSQLConnection m_conn; from the function. 2. Add it to your class definition. i.e.

                  class CYourDialog : public CDialog
                  {
                  public:
                       // Blah, blah, blah....
                   
                  protected:
                       // Blah, blah, blah....
                   
                  private:
                       CSQLConnection m_conn;
                       // Blah, blah, blah....
                  };
                  

                  Notice I added the member variable as private, this is good form - As a rule of thumb always apply the most restrictive access permissions possible. You can always relax them later if need be. Now you can access the variable "m_conn" anywhere in that class. It's not a global however as it can't be accessed from other classes or global functions. Only use the "m_" wart on member variables (variables declared in the class definition) or else you'll confuse people. Steve

                  V 1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    The "m_" wart is normally used to denote a class member variable and not a local variable as you’re using it. Follow these steps: 1. Remove the CSQLConnection m_conn; from the function. 2. Add it to your class definition. i.e.

                    class CYourDialog : public CDialog
                    {
                    public:
                         // Blah, blah, blah....
                     
                    protected:
                         // Blah, blah, blah....
                     
                    private:
                         CSQLConnection m_conn;
                         // Blah, blah, blah....
                    };
                    

                    Notice I added the member variable as private, this is good form - As a rule of thumb always apply the most restrictive access permissions possible. You can always relax them later if need be. Now you can access the variable "m_conn" anywhere in that class. It's not a global however as it can't be accessed from other classes or global functions. Only use the "m_" wart on member variables (variables declared in the class definition) or else you'll confuse people. Steve

                    V Offline
                    V Offline
                    viperlogic
                    wrote on last edited by
                    #9

                    perfect many 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