CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    20

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    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

  4. #4
    Join Date
    Jan 2009
    Posts
    20

    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.

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    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.

  6. #6
    Join Date
    Jan 2009
    Posts
    20

    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
  •  





Click Here to Expand Forum to Full Width

Featured