Archived

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

matt_j

static variables

Recommended Posts

Are using static variables a good programming practice? I am currently using them for small tasks like object animation... well, anyway, are they relitively fast on a memory and processor level (I am also using them inside member functions)?

Share this post


Link to post
Share on other sites
if they are class instances and have constructors: statics will be faster, but you won't notice the difference unless construction is expensive
if they are built-in types or pod types: there will be no appreciable difference in performance

note that static variables make your function non-reentrable (it cannot call itself, directly or indirectly, and it cannot be executed by multiple threads simultaneously*).

use static variables when it makes sense, that is, when you want to persist state across function calls. be aware of restrictions that statics impose on the use of your function.

if you can choose between statics and locals, go for locals. it will make your code more usable (see above) if you or someone using your code ever want to use recursion or multithreading.

* unless calls are synchronized.

[edited by - niyaw on January 29, 2003 1:13:13 AM]

Share this post


Link to post
Share on other sites