• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • 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.
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!!!
1 Solution
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
        int m_nVal;

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

cplauAuthor Commented:
very helpful
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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