-
October 30th, 2009, 09:54 AM
#1
external or global function problem
Hi all,
I am using MFC dialog application and I have some global variables.
I am struggling with where and how I can declare an external function to make initialization for external or global variables and this function should bring the values from DB to store them in those global variables.
I tried to put the desired code without include them inside function structure after the ProjectNameApp theApp in the ProjectName.cpp// but failed
I tried to put the function declaration as a member function in the ProjectNameApp class and call it after the ProjectNameApp theApp in the ProjectName.cpp //but failed as well
I tried to put the function declaration as a member function in the ZZ.cpp and made the proper including //but failed and inform me that you can't initialize them into closed scope.
it seems to me that the initialization should be in the test.cpp and without function using. I don't believe that but how I can make it.
Please any one can help me?
Any help will be appreciated
-
October 30th, 2009, 11:44 AM
#2
Re: external or global function problem
First of all, you should have posted this in the Visual C++ section, since your question is related to MFC, which is only available in MS Visual C++.
For clarity, I would create a single function that initializes all the globals. You can call that function from the InitInstance function in your CWinApp derived class.
Having a lot of global variables, however, is often a sign of poor design. Since you already have a MFC dialog application, there is exactly one application class that will be instantiated and normally there is one dialog class that is used throughout the lifetime of the application. Both could be good candidates to hold the variables that are now global.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 30th, 2009, 12:03 PM
#3
Re: external or global function problem
What D said. Also, if you have multiple instances of the dialog class, you can always make those variables static in the class, that way you still have only one copy of your globals for every instance.
Viggy
-
October 31st, 2009, 06:36 AM
#4
Re: external or global function problem
Thank you for all of you .
Its not working when I make the initialization in a Dialog class and in the Initinistance and even in the constructor of ProjectName class.
but its working when Iam using this:
class CYourApp : public CWinApp
{....
public: float m_Yourvariable;
public: void YourFunction();
...}
In the ZZ.cpp file: I have to make include "YourApp .h"
And I accessed my global variable ---> theApp.m_Yourvariable;
access my global function ---> theApp.YourFunction();
But I am not sure if these variables will woring as global or reserve its values during the cycle of the system.
I will test it and again thank you for all of you.
-
October 31st, 2009, 10:14 AM
#5
Re: external or global function problem
>>>> But I am not sure if these variables will woring as global or reserve its values during the cycle of the system.
Surely they will. The application class will be instantiated directly from main function and is a singleton. When you close the dialog the control will go back to YourApp::InitInstance which will return FALSE such exiting the program. But in any phase of teh program theApp.m_YourVariable will be available.
-
October 31st, 2009, 10:39 AM
#6
Re: external or global function problem
its working fine you are right
Thanks alot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|