static extern?

G

Gernot Frisch

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
 
M

Mark Warren

Gernot said:
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.
<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
 
G

Gernot Frisch

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?
-Gernot
 
S

Sharad Kala

Gernot Frisch said:
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.
 
R

Rolf Magnus

Gernot said:
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.
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.
 
A

Andrey Tarasevich

Gernot said:
...
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.
 
H

Howard

Rolf Magnus said:
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
 
R

Rolf Magnus

Howard said:
<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.
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".
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".
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top