• Advertisement

Archived

This topic is now archived and is closed to further replies.

Can you have extern static variables?

This topic is 5922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This seems like it would be a common question, so forgive me if it has been asked before. I am creating my own sprite class, and throwing it in a header. Well, I want to have a static variable that keeps track of how many vertices are created so I can just use it when I set up my vertex buffer. So each time a new sprite is created, this number is increased by 6 (I know I can do it with 4, but that''s not important right now). Well, the problem arises here. Static files are only visible in the file they are created in, according to the help files of VC++. So I tried to declare the variable like this: extern static int numOfVertices; // I also swapped the first two around to do: static extern int numOfVertices; I get an error saying "more than one storage class specified." Is there any way to work around this? Thanks for your help.

Share this post


Link to post
Share on other sites
Advertisement
You cannot have a "extern static" variable. There''s no reason you''d need to either. You make a variable global variable "static" to basically make it so that it cannot be accessed through extern. You make a variable extern to basically access that variable from another file.

Share this post


Link to post
Share on other sites
Umm, I seem to be a little lost here. First of all, there is a reason I''d want to do this. It is explained in my first post. It''s a clean way to keep track of all the vertices to make room for in the buffer. And the reason I make it static isn''t to prevent a variable from being extern, but rather, to make it so that there is only one variable for my sprite class.

It seems that my method isn''t possible, so I guess I''ll have to have a separate variable that keeps track of how many vertices. Eww.

--Vic--

Share this post


Link to post
Share on other sites
Declaring a (non-extern) static variable in a header will create multiple instances of that variable (one for each source-file the header is included in).

Declaring a (non-static) extern variable in a header and defining it in *one* source file will cause every source-file that includes the header to use the same instance of the variable. This sounds like what you want to do.

The most clean way is perhaps to encapsulate it in the Sprite class though, and avoid having globals. A static class-member variable is shared between all instances of the class, so it should work for what you are trying to do (if I haven''t misunderstood what you''re attempting to do).

Share this post


Link to post
Share on other sites
Dactylos:

What I did is exactly your last suggestion. I have my sprite like this:

class sprite
{
public:
static int numOfVertices;
// bla bla bla
}

However, in my initialize file, when I try to access the variable like this:

sprite::numOfVertices

I get an error that reads:

InitTerm.obj : error LNK2001: unresolved external symbol "public: static int sprite::numOfVertices" (?numOfVertices@sprite@@2HA)

Share this post


Link to post
Share on other sites
If I create an instance of the class and then delete that instance and later create a new one, will the variable defined as static keep the value it had when the previous instance was deleted?

Share this post


Link to post
Share on other sites
To WebSnozz: static member belobg to the class, not to an object. There is only one static member per class and it exists even if you don''t have any instance of the class.
When using static to indicate a static member, the variable is visible in other modules. There shouldn''t be any problem to access it from other cpp files.

Share this post


Link to post
Share on other sites
Thoooms:
From the VC++ help:

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified.

So there is no need to initialize the variable. It is done automatically.

VolkerG:


Again from the VC++ help:

When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

This is at question because this is when talking about regular variables and not member variables. But I''d imagine it wouldn''t be too much different. And there is a problem because I am getting the error when I try to access the variable.

--Vic--


Share this post


Link to post
Share on other sites
quote:
Original post by Roof Top Pew Wee
So there is no need to initialize the variable. It is done automatically.

I wish people would know what they were talking about. static has a totally different meaning when used in conjunction with a class data member (not just a "variable"). Thooms is correct.

quote:

This is at question because this is when talking about regular variables and not member variables. But I''d imagine it wouldn''t be too much different. And there is a problem because I am getting the error when I try to access the variable.

It''s totally different.

From the same MSVC documentation:
In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.

// C++ only
class SavingsAccount
{
public:
static void setInterest( float newValue ) // Member function
{ currentRate = newValue; } // that accesses
// only static
// members
private:
char name[30];
float total;
static float currentRate; // One copy of this member is
// shared among all instances
// of SavingsAccount
};

// Static data members must be initialized at file scope, even
// if private.

float SavingsAccount::currentRate = 0.00154;


''Nuff said.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites

  • Advertisement