Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / c++ gurus: extern static? Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Nils Pipenbrinck

April 16, 2005, 06:29 AM

I run into a problem building a simple menu structure for a game. Here are the guts:

  1.  
  2. struct foo
  3. {
  4.   char       * m_string;
  5.   struct foo * m_link;
  6. };
  7.  
  8. extern foo bar2[];
  9.  
  10. // two arrays that cross-reference each other:
  11. foo bar1[] =
  12. {
  13.   { "item1", &bar2[0] },
  14.   { "item2", 0 },
  15. };
  16.  
  17. foo bar2[] =
  18. {
  19.   { "item3", &bar1[0] },
  20.   { "item4", 0        },
  21. };
  22.  


The code compiles fine. However I don't want to pollute my namespace and export the bar1 and bar2 symbols. So I tried to put a static in front of it. Now the compiler complains with the error "more than one storage class specified".

I have to agree that "extern static" doesn't really makes much sense in general, but in this case I can't define the two arrays without exporting them. How should I write a prototype for a static variable definition without using "extern static"?

It works with functions because you don't need the extern-keyword, but with simple variables I'm lost.

At the moment I've solved the problem by wrapping the definition into a anonymous namespace, but I still think this should somehow work.

Any ideas?

 
fruit

April 16, 2005, 07:31 AM

struct foo
{
char * m_string;
struct foo * m_link;
};

extern foo bar2[];

// two arrays that cross-reference each other:
static foo bar1[] =
{
{ "item1", &bar2[0] },
{ "item2", 0 },
};

static foo bar2[] =
{
{ "item3", &bar1[0] },
{ "item4", 0 },
};

 
juhnu

April 16, 2005, 08:53 AM

A more dynamic approach might be better and would keep you off from hacking things. For me it seems you are trying to implement some sort of ad hoc static linked list.

I might be wrong of course, it's bit hard to guess true intentions from the 10 lines of code :)




juhani

 
IronChefC++

April 16, 2005, 05:07 PM

I don't think it's a linked list. My guess it would be some kind of set of menus. An item on the first menu makes the second menu active. And item on the second menu will return control to the first menu. It's not a linked list in this case but instead a directed graph.

Just remove the extern keyword from the forward declaration of bar2[]. I know for a fact you are allowed to write a declaration for a function without the extern keyword. My compiler (VC7.1) accepts data non-extern data declarations. I haven't consulted the stardard, but I would expect that it should be perfectly acceptable.

 
Danny Chapman

April 16, 2005, 06:28 PM

extern Type var;
tells the compiler that var is a variable of type Type, but that storage has been allocated for it elsewhere.

Type var;
tells the compiler that var is a variable of type Type AND that the compiler should allocate storage for it. (so IronCheckC++'s answer will cause an error, I think)

static Type var;
tells the compiler that var is a variable of type Type AND that the compiler should allocate storage for it, but that it should hide/obfuscate the symbol name so that (a) it can't be accessed from outside the file (actually, translation unit so including all the headers etc included before/afterwards) and (b) you don't get clashes between files that declare different variables with the same name at global scope.

Anyway, that's why fruit's answer is correct.

 
Nils Pipenbrinck

April 17, 2005, 01:08 PM

cool.

I wasn't aware that I can just ditch the static in the prototype. Good to know! Thanks folks.

 
This thread contains 6 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.