Solved

Global Variable

Posted on 1998-09-06
2
203 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
method notes when mouse over in eclipse 5 90
post4 challenge 28 113
Problem to open Excel file 15 227
Bartender label printing - switch on and off graphics 3 77
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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