jonathanasdf
jonathanasdf

Reputation: 2894

Advantage of using a static member function instead of an equivalent non-static member function?

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?

Upvotes: 11

Views: 18059

Answers (4)

Ramanand Yadav
Ramanand Yadav

Reputation: 329

Static function: Advantage:

  1. it could be called without existing real object
  2. Static member functions become useful when you want the class as a whole to have a function, instead of each individual object. With a static member function, you can then change static variable data.

Disadvantage:

  1. it doesn't have pointer to real object ("this")

For example: "singleton class" (with private constructor) is great example of static function advantage using.

Upvotes: 0

Potatoswatter
Potatoswatter

Reputation: 137930

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.

Upvotes: 4

Mike DeSimone
Mike DeSimone

Reputation: 42825

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.

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 186098

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.

Upvotes: 13

Related Questions