how to use extern variable

H

Han

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?
.......
 
W

WaterWalk

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top