MFC Global Variable

Where in the MFC class can I save/put a variable that will be accessible to all classes (Derived Dialog and other other classes added to an MFC project)

Comments

  • : Where in the MFC class can I save/put a variable that will be accessible to all classes (Derived Dialog and other other classes added to an MFC project)
    :


    Sometimes I put them in the CWinApp-derived class because that class is accessible to all other classes.
    [code]
    class CMyApplication : public CWinApp
    {
    public:
    int some_global_variable;
    }


    int CAnotherClass::GetSomeVariable()
    {
    CMyApplication* pApp = (CMyApplication *)AfxGetApp();
    return pApp->some_global_variable;
    }
    [/code]
  • : : Where in the MFC class can I save/put a variable that will be accessible to all classes (Derived Dialog and other other classes added to an MFC project)
    : :
    :
    :
    : Sometimes I put them in the CWinApp-derived class because that class is accessible to all other classes.
    : [code]
    : class CMyApplication : public CWinApp
    : {
    : public:
    : int some_global_variable;
    : }
    :
    :

    : int CAnotherClass::GetSomeVariable()
    : {
    : CMyApplication* pApp = (CMyApplication *)AfxGetApp();
    : return pApp->some_global_variable;
    : }
    : [/code]
    :
    If they are more than one does than mean I'll have to write diff fxns for each global variable.
    Then the AfxGetApp() fxn, can I also use it to get a pointer to another class (like getting a member variable of one CMyAppDialog1 to another CMyAppDialog2).
  • : :
    : If they are more than one does than mean I'll have to write diff fxns for each global variable.
    [blue]No -- I just showed an example of how to access the variable from any other class.[/blue]

    : Then the AfxGetApp() fxn, can I also use it to get a pointer to another class (like getting a member variable of one CMyAppDialog1 to another CMyAppDialog2).
    :

    [blue]AfxGetApp() only get the pointer to the CWinApp-derived class. You can't use it for anything else.[/blue]
  • : Where in the MFC class can I save/put a variable that will be accessible to all classes (Derived Dialog and other other classes added to an MFC project)
    :
    I would introduce some really global variable, like
    [blue][b]int[/blue] MyVariable;[/b]
    in some file, say MyApp.cpp, and declare it in the header for that file:
    [blue][b]extern int[/blue] MyVariable;[/b]
    That's all. Object oriented people would not like it, but it is straightforward and does not execute a function. Works even with pointers to classes, just the header must be known at usage.




  • Another possibility you can try , is using statics variables ...
  • [b][red]This message was edited by AsmGuru62 at 2005-3-4 18:32:33[/red][/b][hr]
    [blue]No need to cast or call a function. There is a global variable already called 'theApp' and it is of your CApplication derived class. Just use some include file which has this line:
    [code]
    extern CMyApp theApp; // Copy/Paste it from the file where it is
    //declared and add 'extern' before it.
    [/code]
    [/blue]



  • : [code]
    : extern CMyApp theApp; // Copy/Paste it from the file where it is
    : //declared and add 'extern' before it.
    : [/code]
    : [/blue]
    :
    :
    :

    AfxGetApp() returns a pointer to theApp and I think it is the preferred way to access it.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories