 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gernot Frisch Guest
|
Posted: Mon Jun 28, 2004 9:21 am Post subject: static extern? |
|
|
Problem:
A.cpp:
------
static FOO* gFoo=NULL;
A.h
extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
if A.h looks like:
extern static FOO* gFoo;
it gives:
C2159: more than one storage class specified.
<blink, blink>
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.
Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
|
|
| Back to top |
|
 |
Vince Yuan Guest
|
Posted: Mon Jun 28, 2004 9:53 am Post subject: Re: static extern? |
|
|
Try this:
A.h
static FOO* gFoo;
A.cpp:
extern static FOO* gFoo=NULL;
Vince
"Gernot Frisch" <Me (AT) Privacy (DOT) net> wrote
| Quote: | Problem:
A.cpp:
------
static FOO* gFoo=NULL;
A.h
extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
if A.h looks like:
extern static FOO* gFoo;
it gives:
C2159: more than one storage class specified.
blink, blink
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.
Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
|
|
|
| Back to top |
|
 |
Mark Warren Guest
|
Posted: Mon Jun 28, 2004 10:15 am Post subject: Re: static extern? |
|
|
Gernot Frisch wrote:
| Quote: | Problem:
A.cpp:
------
static FOO* gFoo=NULL;
A.h
extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
if A.h looks like:
extern static FOO* gFoo;
it gives:
C2159: more than one storage class specified.
|
Yes, the variable gFoo cannot be static and extern at the same time.
| Quote: | blink, blink
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.
Thank you,
|
Solution:
A.cpp:
------
FOO* gFoo=NULL; // Note no static
A.h
----
extern FOO* gFoo;
HTH
Mark
|
|
| Back to top |
|
 |
Gernot Frisch Guest
|
Posted: Mon Jun 28, 2004 11:43 am Post subject: Re: static extern? |
|
|
| Quote: | Solution:
A.cpp:
------
FOO* gFoo=NULL; // Note no static
A.h
----
extern FOO* gFoo;
|
I rewrote the code so I can do this now. But I don't understand it.
Why can't I tell another .cpp file that there is an static variable
somewhere else?
-Gernot
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Mon Jun 28, 2004 11:48 am Post subject: Re: static extern? |
|
|
"Gernot Frisch" <Me (AT) Privacy (DOT) net> wrote
| Quote: | Solution:
I rewrote the code so I can do this now. But I don't understand it.
Why can't I tell another .cpp file that there is an static variable
somewhere else?
|
Because static variables having namespace scope have internal linkage i.e.
the entity it denotes can be referred to by names from other scopes in the
same translation unit only.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Mon Jun 28, 2004 12:33 pm Post subject: Re: static extern? |
|
|
Gernot Frisch wrote:
| Quote: | Problem:
A.cpp:
------
static FOO* gFoo=NULL;
A.h
extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
|
Well, static means internal linkage, extern means external linkage. You
have to choose one. If the variable is static, it doesn't make much
sense to declare it as extern in the header.
| Quote: | if A.h looks like:
extern static FOO* gFoo;
it gives:
C2159: more than one storage class specified.
blink, blink
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.
|
Why? It's static, so it doesn't exist outside of the .cpp file where
it's defined.
|
|
| Back to top |
|
 |
Gernot Frisch Guest
|
Posted: Mon Jun 28, 2004 1:02 pm Post subject: Re: static extern? |
|
|
| Quote: | Why? It's static, so it doesn't exist outside of the .cpp file where
it's defined.
|
Because: That is exactly what I didn't know. Thank you,
Gernot
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Mon Jun 28, 2004 6:39 pm Post subject: Re: static extern? |
|
|
Gernot Frisch wrote:
| Quote: | ...
Why can't I tell another .cpp file that there is an static variable
somewhere else?
|
Because that what this particular use of 'static' is intended to do in
the first place! It is intended to make the variable "invisible" from
other translation units. If you want to be able to link to that variable
from other translation units - give it external linkage.
--
Best regards,
Andrey Tarasevich
|
|
| Back to top |
|
 |
Jack Klein Guest
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Tue Jun 29, 2004 10:29 pm Post subject: Re: static extern? |
|
|
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
| Quote: | Gernot Frisch wrote:
Problem:
A.cpp:
------
static FOO* gFoo=NULL;
A.h
extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
Well, static means internal linkage, extern means external linkage. You
have to choose one. If the variable is static, it doesn't make much
sense to declare it as extern in the header.
|
<rant>
You know, I can't for the life of me figure out why "static" was used to
mean internal linkage in this case. I'd think "intern" or something similar
would be much more logical, and simply eliminate the use of "static" for
non-member functions and variables. I know this is not the forum for asking
"why", but it really bugs me to have a keyword that has no apparent relation
to its use. One would think that "static" should have the same (or at least
similar) meaning in this context as it does in member functions and
variables. And since it makes no sense to try to apply the same meaning in
this case as in the member (or local variable) case, it really just
shouldn't exist at all here. "But that's just my opinion...I could be
wrong."
</rant>
-Howard
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Jun 30, 2004 10:20 pm Post subject: Re: static extern? |
|
|
Howard wrote:
| Quote: | Well, static means internal linkage, extern means external linkage.
You have to choose one. If the variable is static, it doesn't make
much sense to declare it as extern in the header.
rant
You know, I can't for the life of me figure out why "static" was used
to
mean internal linkage in this case. I'd think "intern" or something
similar would be much more logical, and simply eliminate the use of
"static" for non-member functions and variables.
|
I think they didn't want to introduce a new keyword. Also, they wanted
to remain compatible to C, which alread had defined it this way. And
actionally the meaning that static has within a class definition is the
youngest one.
| Quote: | I know this is not the forum for asking "why", but it really bugs me
to have a keyword that has no apparent relation to its use.
|
I had more problems with the syntax they chose for pure virtual
functions. "Initializing" a function declaration with 0 looks very odd
to me. But again, I suspect they didn't want to add a new keyword
"pure".
| Quote: | One would think that "static" should have the same (or at
least similar) meaning in this context as it does in member functions
and variables.
|
It depends on how you look at it. If you look at it the right way,
static has more or less always the same meaning.
Within a class, static means that the function or variable is only there
once, for the class, not for each instance of it. Same for a local
static variable, if you count calling a function as "instantiating" it.
Well, and a static variable/function on namespace scope is there for
this one translation unit. It always has a meaning similar to "there is
only one".
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|