static variable

J

junw2000

For c language, a static variable can only be used within the file in
which it is defined.
But for c++, a class's static data member can be used in other files.
Is it contradictory?

Thanks
 
A

Alf P. Steinbach

* (e-mail address removed):
For c language, a static variable can only be used within the file in
which it is defined.
But for c++, a class's static data member can be used in other files.
Is it contradictory?

It is a misconception on your part.

I think what you're looking for are the terms "internal linkage" and
"translation unit" a.k.a. "compilation unit".

They don't involve files: files are the way you physically package a C++
program's source code.


Cheers, & hth.,

- Alf
 
J

Juha Nieminen

For c language, a static variable can only be used within the file in
which it is defined.
But for c++, a class's static data member can be used in other files.

In other files than what?
 
J

James Kanze

For c language, a static variable can only be used within the file in
which it is defined.
But for c++, a class's static data member can be used in other files.
Is it contradictory?

Sort of. In C++, the keyword static is overloaded, depending on
context. When applied to a variable at namespace scope, it
means internal linkage, i.e. that the same name in another
translation unit refers to a different entity. When applied to
a variable at class scope, it means that the variable exists
once, independently of any instance of the class---all class
members always have the same linkage as the class (which is
external unless the class is local). For local variables, the
keyword also has no effect on linkage. (The meaning of static
for local variables and class variables is actually somewhat
related: one instance, present always.)
 

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
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top