| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
28 Jan 2008 01:09:13 AM |
| Object: |
static variable |
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
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: static variable |
28 Jan 2008 01:20:07 AM |
|
|
* junw2000@gmail.com:
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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
| User: "Tim Love" |
|
| Title: Re: static variable |
28 Jan 2008 04:49:27 AM |
|
|
writes:
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's a bit confusing, yes. See
http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/static.html
for examples.
.
|
|
|
|
| User: "Juha Nieminen" |
|
| Title: Re: static variable |
28 Jan 2008 01:30:55 AM |
|
|
wrote:
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?
.
|
|
|
|
| User: "James Kanze" |
|
| Title: Re: static variable |
28 Jan 2008 04:36:30 AM |
|
|
On Jan 28, 8:09 am, wrote:
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.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|

|
Related Articles |
|
|