Solved

Global Variable

Posted on 1998-09-06
2
199 Views
Last Modified: 2013-11-20
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
Comment
Question by:cplau
2 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 20 total points
ID: 1321808
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
 

Author Comment

by:cplau
ID: 1321809
very helpful
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 153
fizzArray2 challenge 1 72
Unix Command -- Challenging  question 7 87
Problem to open Excel file 15 99
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now