开发者

Shall I make every function static, which do not touch the member variable?

What is the Design and Performance impact for making all functions static which do not touc开发者_运维百科h the member variable of the class?


You should actually consider making them non-static free functions, as explained in detail in this question. This question is also very interesting.

In a nutshell, these question explain that you should prefer non-friend non-member functions whenever possible (meaning when they do not access non-public members).


What are Design and Performance impact for making all function static which do not touch member variable of class?

performance: static member functions may be slightly faster than non-static member functions because they don't need to pass a this pointer, but you're unlikely to notice the difference; where inlining is used there may not be one. Further, pointers to a static function may be used directly, whereas "pointers" to non-static member functions are typically offsets/indices and require a this pointer for use; the run-time CPU operations involved can be expected to be slightly more complicated.

design: the choice between static and non-static member function can safely be made on the basis of the need to access an object's non-static member data in order to fully perform the expected operation. If you're generally comfortable with OOP and it doesn't seem intuitive and sensible to call the function using the notation object.fn(x, y, z) - that the function lends itself to being perceived as an operation on the current state of that specific object - then it probably shouldn't be a non-static member.

Ignoring the question as I understand it and looking at the wider terrain, free functions do have their own advantages as discussed in other replies; countering that the tighter association of static members can help programmers find potentially useful routines - all depending on the tools and habits they have.


Performance-wise, static member functions are faster and use less stack space because they do not need to pass a this pointer. But this isn't a significant cost.

Regarding design, you should ask yourself why the functions are members of the class if they do not access its data members? There certainly are design patterns that include static functions. However, a widely favored approach to class design is to choose the minimum number of functions necessary to expose the functionality of the class while keeping its data hidden. This makes it easier to change the internals of the class without knock-on changes to the code which uses the class. Such an approach has little use for static functions as they cannot provide access to the data.


Absolutely yes. The non-static member functions are meant to cater the non-static member variables. If variables are not used then the function should be made static which makes your design cleaner and you can avoid passing this as the 1st hidden argument (which betters the performance a little).

[Note: On funny side there is one notable exception:

struct A {
  virtual void foo (int) = 0;
};

Even though you don't have member used inside foo() you can't make it static ! :)]


Sometimes it makes sense to make a function virtual even if it does not access any instance members - for example to return some class-related properties (something like virtual bool eatsPlants() in the animal class hierarchy). Then it cannot be static because there are no virtual static members in C++.


Performance of static member functions vs free functions?
There is absolutely no performance difference between static member functions and free functions.

Performance of static member functions vs non static member functions?
Typically static member function are used to eliminate the need for an object and eliminate the extraneous this argument and that is the only performance advantage over non static member functions but it is hardly noticeable.


It depends. First, of course, if the function is to be virtual, then it can't be static. (Most of the member functions I have which don't touch a member variable are virtual. Otherwise, why make it a member at all?) Otherwise, it depends on the role of the function in the class. If it is fundamentally independent of any instance of the class (e.g. it sets some global parameter of the class, which affects all instances), then it should be static; such functions should be fairly rare, however. If on the other hand, it only incidental that it doesn't touch a member variable;, if conceptually, it does involve the specific instance, then make it a member. (This is frequent in the case of virtual functions, but probably very rare otherwise.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜