How can I define a variable, e.g. "MYPROC MyProc;" so that it can be used in
everywhere, in Main.cpp, or Blahblah.cpp?
I've tried to declare this variable in a header file, then having both
Main.cpp and Blahblah.cpp including this header file, but seems it is not OK
to do so, how can I fix this problem?

I appreciate any help.

David

Re: global variables by Sreeram

Sreeram
Thu Aug 14 03:54:16 CDT 2003

declare it in main.cpp. say "int i;"
declare as "extern" in your blahblah .cpp "extern int i;" (just an example)

Sreeram


David Lau wrote:
> How can I define a variable, e.g. "MYPROC MyProc;" so that it can be used in
> everywhere, in Main.cpp, or Blahblah.cpp?
> I've tried to declare this variable in a header file, then having both
> Main.cpp and Blahblah.cpp including this header file, but seems it is not OK
> to do so, how can I fix this problem?
>
> I appreciate any help.
>
> David
>
>


Re: global variables by Kanon

Kanon
Thu Aug 14 08:22:28 CDT 2003

// ******************
// SomeFile.h
// ******************

// extern declaration lets everyone know
// that it's around *somewhere*
extern MyClass objMyClass;



// ******************
// SomeFile.cpp
// ******************

// This actually allocates memory
// and creates the class
MyClass objMyClass;






"David Lau" <ywcs4521@hotmail.com> wrote in message
news:eQR2I$jYDHA.2476@tk2msftngp13.phx.gbl...
> How can I define a variable, e.g. "MYPROC MyProc;" so that it can be used
in
> everywhere, in Main.cpp, or Blahblah.cpp?
> I've tried to declare this variable in a header file, then having both
> Main.cpp and Blahblah.cpp including this header file, but seems it is not
OK
> to do so, how can I fix this problem?
>
> I appreciate any help.
>
> David
>
>



Re: global variables by Steve

Steve
Thu Aug 14 10:12:07 CDT 2003

> declare it in main.cpp. say "int i;"
> declare as "extern" in your blahblah .cpp "extern int i;" (just an
example)
Don't do this!
This allows errors such as:
main.cpp: int i;
blahblah.cpp extern float i:
Type mismatch not detected until some runtime error.

Follow Kanon Wood's example.
One extern declaration, in a header file.
One definition in a .cpp file.
Include the header in all .cpps the use the variable, including
the one that defines it.
Compiler will tell you if there is a mismatch.

HTH,
Steve