generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Question about static member variables

Previous Thread :: Next Thread 
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
11 Apr 2012 06:48 PM
Why do they have to be declared in file scope? Can't regular static variables be declared inside functions?
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
11 Apr 2012 06:50 PM
Wait, static variables in function are different from static variables in classes which are different from static variables in global scope... which ones do you mean when "declared in file scope"?
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
11 Apr 2012 06:52 PM
Oh, static variables in classes. I get errors when it try to declare their value in a function.
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
12 Apr 2012 02:07 PM
I think I understand it a lil better after doing some more research. It seems that all static variables declared, must also be given a value somewhere in the file scope.

The last part is what I do not understand: why it MUST be given a value in the file scope, and not in some function. After all, regular static variables can be declared and initialized anywhere.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
12 Apr 2012 05:36 PM
What language are you talking about?
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
12 Apr 2012 05:42 PM
"What language are you talking about?"

C++, from his group.
Report Abuse
Legend26 is not online. Legend26
Joined: 08 Sep 2008
Total Posts: 10586
12 Apr 2012 06:00 PM
I have no idea, but for static class variables, I usually just initialize them right after the class def.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
12 Apr 2012 06:16 PM
"Why do they have to be declared in file scope?"

Not sure what you mean. If they were declared at the file scope, they wouldn't be member functions. I'm guessing you mean _implemented_ at the file scope. Well, strictly speaking there's no reason that they have to be, but in generaly you follow the declaration-in-the-header + implementation in the source file pattern. For static members you have to implement them in the source file for the exact same reason that you want to implement methods in the source file: linkage. If you had multiple source files which included the implementation of a static variable which were instead fully defined in the class in a header, the compiler would end up generating multiple copies of the location used to store the static member. But then when the file is linked how would the linker know which to use? The solution is to only implement the static var in a single separate source file. The same goes for methods too...

But then you may ask, why can I put some methods in the class declaration just fine? Well, you may not have known but doing that actually has a special meaning, it forces the compiler to inline those methods rather than generating a single piece of code which all of the call sites refer to. That's why you should never put really big methods in the header file, it will increase the size of your generated executable (It may even decrease the speed in contrived enough cases, since it means that there's more code that has to compete for space in the instruction cache).

"Can't regular static variables be declared inside functions?"

Static has a different meaning inside of a function body. If you declare a local variable as static it will be a static variable instead, (that is, only one copy of it exists for the whole run-time of the program, and it maintains it's value between calls) but will not be visible outside of the function's scope.

Another note, you might see global vars not in classes declared this way, and these work in the exact same way as the members that I just described.

Another another note: If you see a global function declared as static that does NOT mean the same sort of thing. This has yet another meaning, it reduces the "visibility" of the function to only the source file that it's defined in.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
12 Apr 2012 06:17 PM
"I have no idea, but for static class variables, I usually just initialize them right after the class def."

That will break with a link error "multiple definitions of ..." as soon as you try to do anything non-trivial.
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
12 Apr 2012 06:47 PM
@stravant,

Okay, so pretty much static members are different than static variables, and the main reason they must be initialized in file scope is because they are made for only one declaration?
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
12 Apr 2012 07:27 PM
Sorry for not replying to this for a while.

Stravant seems to have explained it well, but basically static member variables need to exist somewhere, in some source file, because they're not in class instances but are actually global variables... very similar to global variables in namespaces.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
12 Apr 2012 10:58 PM
"they must be initialized in file scope is because they are made for only one declaration?"

Careful with your terminology. You can "declare" anything as many times as you want in C++, the declaration is just to reserve spaces in the namespace and tell the compiler the structure of the stuff that it's working with. So no, they aren't "made for one declaration".

What I'm guessing you meant to say is that they're made for one _implementation\initialization_, and unfortunately that's not exactly true either. It's fine to say that the "typical usage" is for a static variable to have only one initialization, however, it's possible for your codebase to have more than one initialization of a static variable _as long as_ you only link one of them into the final executable, it's not possible to link multiple initializations into the same executable for the same static variable.

This is even a very reasonable thing that you might want to do in real code. For instance, take the case where you may have varying values for a given variable depending on which OS you're running on, or whether you're running in debugging mode or not.(not the most realistic examples since you would probably just conditionally include code during a recompile instead) You could accomplish this without needing a full recompile by simply linking in different copies of the library to change what value the variable has.
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
13 Apr 2012 02:22 PM
Wait, so I can do this?

class X
{
public:
static int lol;
static int lol;
}

and then somehow link only one of them in the final executable?

Also, initializing static members in the file scope and not in the function scope is mainly for differentiating between static variables? If so, that seems silly..
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
13 Apr 2012 05:49 PM
"Also, initializing static members in the file scope and not in the function scope is mainly for differentiating between static variables? If so, that seems silly.."

Please please please be careful with your terminology, you keep saying things that make me think you might get the idea but you can't I can't be sure.

I'm not sure what to make of your statement, did you mean the function scope, or the _class_ scope? If you did mean the function scope then no, you obviously don't put the _class_ static members in the function scope because anything in the function scope is local to the function. If you meant the class scope, then yes-ish, you put the things in the scope that you want them to be contained in. Local stuff to a function is declared in that function's body, class data is declared in the class body, etc...

But I still don't get what you mean by saying:
"is mainly for differentiating between static variables?"
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
13 Apr 2012 06:08 PM
Yes, I think I know what I am talking about.. see, this works:

#include "iostream"
using namespace std;

class X
{
public:
X() { ++count; }
int GetCount() { return count; }
static int count;
};
int X::count = 1;

int main()
{
cout << X::count << "\n";
return 0;
}
----------
but this doesn't:

#include "iostream"
using namespace std;

class X
{
public:
X() { ++count; }
int GetCount() { return count; }
static int count;
};

int main()
{
int X::count = 1;
cout << X::count << "\n";
return 0;
}
--------------
I also realized that you gotta re-put the `int` when you give the value of the static member.
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
13 Apr 2012 06:18 PM
The static member variables need to exist (and be declared) in global scope. It's as simple as that. Your second example doesn't work because you can't create the static variable on the stack.
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
13 Apr 2012 06:41 PM
Okay, sorry.
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
13 Apr 2012 06:47 PM
There's nothing to be sorry about.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image