Making an Object global
-
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?
-
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?
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 -
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 summany thanks for your repply could you further explain this with a code example please?? regards
-
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?
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 itstatic
. 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 -
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?
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
-
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?
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
-
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
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
-
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
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 -
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. Steveperfect many thanks