• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Global Variable

Can I delcare a global variable in Viusal C++ 5.0?

I want to have a variable that can be used by two *.cpp files.
eg.
I used the class CListView to display all data in the
Window. After clicking one of the items, I want the value
of this itemc can be displayed on the dialog box.
Until now, I cannot do this in using two *.cpp files.

Thanks for your help!!!
0
cplau
Asked:
cplau
1 Solution
 
chensuCommented:
Yes, you can.

For example,

// 1.h
extern int g_nVal;  // declaration

// 1.cpp
#include "1.h"

int g_nVal = 1;  // definition

// 2.cpp
#include "1.h"

g_nVal = 2;  // reference


Another way is to define it as a member variable of the derived CWinApp class.

class CMyWinApp : public CWinApp
{
//...
    public:
        int m_nVal;
//...
};

((CMyWinApp *)::AfxGetApp())->m_nVal = 2;

0
 
cplauAuthor Commented:
very helpful
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now