Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to use extern variable

Reply
Thread Tools

how to use extern variable

 
 
Han
Guest
Posts: n/a
 
      11-13-2007
If I define a variable in a.cpp as a global variable,but I want to use
it in b.cpp. How to do that?
For example
//a.cpp
vector<int> * p
void main()
{
}

//b.cpp How can I use "p" here?
.......

 
Reply With Quote
 
 
 
 
WaterWalk
Guest
Posts: n/a
 
      11-13-2007
On Nov 13, 5:31 pm, Han <(E-Mail Removed)> wrote:
> If I define a variable in a.cpp as a global variable,but I want to use
> it in b.cpp. How to do that?
> For example
> //a.cpp
> vector<int> * p
> void main()
> {
>
> }
>
> //b.cpp How can I use "p" here?
> ......


Use "extern" to declare this variable before you use it. For example
in b.cpp, you can write this:
extern vector<int> *p;
//use p...

Generally, an extern declaration is put in a header file, so that it
can be easily used by other files.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
extern gloabal variable in .h Possible to use in .c withoutincluding .h knight C++ 6 02-17-2011 11:33 AM
extern global variable defined within namespace Dan Elliott C++ 4 12-08-2004 10:21 AM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM



Advertisments