• Advertisement
Sign in to follow this  

Discussion about the Static keyword

This topic is 1928 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

Im sure this keyword applies to many languages. It is the accessing of methods/fields/etc at the "class definition"? level.

I am about to rewrite most of my game after realising that the static keyword is, well, not logical in my case (I think atleast). I wrote many of my classes static - most of these classes were classes that I thought would only be initalized once. Although after realising how easy it would be to just recreate a corrupted object when a corruption occurs (by corruption I mean basically anything, from a multiplayer connection failing, to a incompatable object state) I was like "WHAT HAVE I DONE?".

[b]What are other programmers thoughts on the static keyword in OOP?[/b]

Sure the static keyword can be extremely helpful in certain cases, but in general what do you think?

Thanks,
Xanather. Edited by Xanather

Share this post


Link to post
Share on other sites
Advertisement
In general static is useful when used as intended - as a property or method of a class of objects.

Note, you say "at the object level" which is wrong - normal non-static properties and methods are properties of [i]objects[/i]; statics are properties of [i]classes of objects[/i]. You clearly know this already, but it's worth getting in the habit of making the distinction correctly :)

It sounds as though you have fallen into the trap of prematurely constraining your design. While static (and Singletons in particular) are often misused in this way, the problem is with the process of design - [url="http://c2.com/cgi/wiki?YouArentGonnaNeedIt"]YAGNI[/url]

Share this post


Link to post
Share on other sites
http://gbracha.blogspot.com/2008/02/cutting-out-static.html

In languages without a good module system (pretty much everything), they are a useful approach for global methods and constants (or near constants) if treated with caution.

Share this post


Link to post
Share on other sites
@mrbastard Yeah.. I knew that. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] I think I just typed it out wrong. edited. and yes I would admit to a bad design. Probably should have thought it through first. Edited by Xanather

Share this post


Link to post
Share on other sites
[quote name='mrbastard' timestamp='1349696266' post='4987954']
In general static is useful when used as intended - as a property or method of a class of objects.

Note, you say "at the object level" which is wrong - normal non-static properties and methods are properties of [i]objects[/i]; statics are properties of [i]classes of objects[/i]. You clearly know this already, but it's worth getting in the habit of making the distinction correctly [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

It sounds as though you have fallen into the trap of prematurely constraining your design. While static (and Singletons in particular) are often misused in this way, the problem is with the process of design - [url="http://c2.com/cgi/wiki?YouArentGonnaNeedIt"]YAGNI[/url]
[/quote]
Good read by the way thanks :)

Share this post


Link to post
Share on other sites
Hi all sure is lots of talk about the static thing. why, why.
I'm Holding all my Game Object Creation and types in a static class.
I did this because Im trying things out. My last game I passed all classes as params and that was a pain Im lazy foo.
So now Im using a ObjectManager Class. Goes something like this.


class cObjManager
{
public:

static vector<obj *>DefinedObj;

cObjManager(){}
~cObjManager(){}

static bool InitObjManager(things)
{
//load all type here and store them in a vector<obj *>
//must allways call this
return true;
}

static obj *CreateNewObj(Params)
{
search static list for the type we want and then create that type and return;
}


};//end class



now all I have to do is add the header to the file I want to use it in.
Call its Inmember once.
And coded like this Obj *newobj = cObjManager::CreateNewObj(Params);

It's so far is saving me time in writing code, looks like its working well so Ill keep going.

Hope that helps.. any way dont not use some part of the language, try it out and find the pittfulls.

Share this post


Link to post
Share on other sites
[quote name='ankhd' timestamp='1349770379' post='4988250']
Hi all sure is lots of talk about the static thing. why, why.
I'm Holding all my Game Object Creation and types in a static class.
I did this because Im trying things out. My last game I passed all classes as params and that was a pain Im lazy foo.
So now Im using a ObjectManager Class. Goes something like this.
[/quote]

Just a few notes on that class.
Since you never create any object, you don't need the constructor/destructor.
The whole class could also be just a couple of functions in a .h-file, and will work identically to that.
If you want the CObjCreator::somemethod-syntax, you could just wrap those functions in a namespace, and write a few lines less of code. (no static and public keywords)
That might also make it a bit more clear why doing it like this isn't really OOP-programming.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement