views:

295

answers:

3

I was wondering whether there's any advantages to using a static member function when there is a non-static equivalent. Will it result in faster execution (because of not having to care about all of the member variables), or maybe less use of memory (because of not being included in all instances)?

Basically, the function I'm looking at is an utility function to rotate an integer array representing pixel colours an arbitrary number of degrees around an arbitrary centre point. It is placed in my abstract Bullet base class, since only the bullets will be using it and I didn't want the overhead of calling it in some utility class. It's a bit too long and used in every single derived bullet class, making it probably not a good idea to inline. How would you suggest I define this function? As a static member function of Bullet, of a non-static member function of Bullet, or maybe not as a member of Bullet but defined outside of the class in Bullet.h? What are the advantages and disadvantages of each?

+4  A: 

There is absolutely no performance difference between static member functions and free functions.

From a design perspective, it sounds like the function in question has very little to do with Bullets, so I would favour putting it in a utility library somewhere, there is no runtime overhead in doing this, only extra developer effort if you don't already have such a library.

Regarding the original question, if the function doesn't obviously pertain to a particular class, then it should be a free function. At the most, it should belong to a namespace, to control its scope. And even if it pertains to a class, most times, I would still prefer the free function unless the function requires access to private members.

Marcelo Cantos
yeah, I'm not really sure how C++ works. I come from a Flash background and ActionScript has horrible overheads in the weirdest places - static functions in utility classes are resolved at runtime, so like Math.sin every time it's used the player has to find the Math class through the hashtable and then find the sin function. Glad to hear that C++ at least has more common sense..
jonathanasdf
@Zan: static member functions don't have a `this` pointer.
Marcelo Cantos
@Marcelo: Indeed you are right. I read the line I was responding to as "There is absolutely no performance difference between member functions and free functions." which isn't actually what it says. My bad.
Zan Lynx
"only" extra developer effort
bobobobo
A: 

If the method needs to be in the class namespace, but does not operate on an instance of that class (i.e. it does not use this nor does it use any non-static methods), it should be declared static.

Off the top of my head, I used static methods a few times when I had a class that kept track of its instances. It used a doubly-linked list, the constructor inserted, and the destructor removed the object from the list. The object itself had the previous and next pointers as members, and the first and last pointers were static members, and all these pointers were private. All the methods that worked on the list, e.g. search or count, were static methods.

Mike DeSimone
A: 

Typically static is used if possible, to eliminate the need for an object and eliminate the extraneous this argument.

But one exception is in functors: classes which define operator() so objects can be "called" as functions. Idiomatically such an operator() is declared inside the class {} block, which makes it inline.

Then, if the function is small, it is inlined into the calling function and the this pointer is optimized out.

If the function is large, it may not be inlined. But the miniscule disadvantage of having an extra argument is probably dwarfed anyway.

Potatoswatter