• Advertisement

Archived

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

static

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

what''s the advantage of making things static in C++ (both functions and vareous variables)? When can it be done and when not?

Share this post


Link to post
Share on other sites
Advertisement
The ''static'' keyword is depreciated in c++, you should use namespaces for organisation instead

Share this post


Link to post
Share on other sites
static also means that the variable maintains it''s value between calls to the function.

I think MrLove is referring to making a global value "static" prevents the variable from being referenced via extern.

Also, it means to have an item shared between classes.

Really, it''s a matter of personal taste. I never really use static locals, but do use them for class members.

Share this post


Link to post
Share on other sites
Here's what Wikipedia has to say about it:

"In object-oriented programming, static refers to a property of an object which belongs to the object's class and not the object itself. This term is not used universally among object-oriented programmers, but is consistent with its use in popular object-oriented programming languages C++ and Java."

I suggest doing a google search for a tutorial like this: http://cplus.about.com/library/weekly/aa080802a.htm

The static local variable thing is also true. I actually use them sometimes as a kludge that I take out soon after. They are pretty much crying alone in the corner with goto and continue most of the time.


[edited by - JeffF on March 5, 2004 8:19:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by JeffF
They are pretty much crying alone in the corner with goto and continue most of the time.



Whoopsy, what''s wrong with continue?

If you go through a for loop with x going from 0 to 64 and y going from 0 to 64, and you don''t want most of the stuff inside the loop to happen under certain circumstances in function of x and y, how else are you going to skip it and try the next? :/

Share this post


Link to post
Share on other sites
Well, if you want to be able to call a function without an actual object, you can either make the function global or static- the two things are equivalent. If it makes sense to associate the function with a class, then it would be better to make it static within the class, from an organization point of view.

I don''t ever use namespaces, I find them pretty troublesome, with compiler-dependant quirks.

Static class variables are like globals, and you should use them for the same reason, good organization.

As for static local variables.. those things can be trouble, I would avoid those things until you really know what you''re doing.

Share this post


Link to post
Share on other sites
quote:
Original post by Boops
...how else are you going to skip it and try the next? :/


Come now, clearly that was an attempt at humor. Mainly I needed three things to be in the corner rather than two. It's just funnier that way.

Some people think that continue is bad style, many people simply aren't in the habit of using it, continue doesn't get used much. Do a google search for "C++ continue bad style" and you will find lots of style guides reccomending against it. I really don't want to argue about it because I don't care much and I'm hungry. Use continue all you want, it isn't hurting me a bit!

[edited by - JeffF on March 5, 2004 10:28:18 PM]

Share this post


Link to post
Share on other sites
continue isn''t really any worse than break. Now I bet there isn''t a non-beginner C++ programmer who hasn''t used break. Admittedly there are far fewer circumstances where you''d want to use it. However when an appropriate situation does arrise (and it does) then it would be foolish to try and do without it. If you haven''t found a place that makes perfect sense to use continue before then you haven''t been programming long enough.

No you wouldn''t catch be using goto though btw.

Local static''s are very much a good thing as well as they prevent other functions from touching them. static functions are supposed to be slightly smaller and faster to call too, and hence are preferred if no other files need to access the function.

Share this post


Link to post
Share on other sites

  • Advertisement