A question about global variables

T

Tony Johansson

Hello experts!

Is it any difference if I instansiate object testobj of class Test as
alternative (1) or (2).
In this case I think it's the same thing. I'm I right.

(1)Test testobj
(2) static Test testobj
main()
{}

Many thanks!
//Tony
 
J

Jason

Tony said:
Hello experts!

Is it any difference if I instansiate object testobj of class Test as
alternative (1) or (2).
In this case I think it's the same thing. I'm I right.

(1)Test testobj
(2) static Test testobj
main()
{}

Many thanks!
//Tony

Yes, and no.

Static global variables have what is called file-level scope. Within
this c++ file you can use either method interchangeably. However, if
you are linking this file with another file, then the statically
declared variable will not be available (without using extern) in that
file.

-Jason
 
J

Jason

Static global variables have what is called file-level scope. Within
this c++ file you can use either method interchangeably. However, if
you are linking this file with another file, then the statically
declared variable will not be available (without using extern) in that
file.

My bad. Using extern makes no difference in this context. A static
global variable is usable from that file, and that file only. I'm
surprised no one jumped on this yet! ;P

-Jason
 

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,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top