What is the use of private static member functions?

前端 未结 5 2062
一生所求
一生所求 2021-01-31 07:37

I was looking at the request parser from the boost::asio example and I was wondering why the private member functions like is_char() are static? :

5条回答
  •  悲&欢浪女
    2021-01-31 08:27

    So is there a reason why these functions are static?

    Non-static member functions have a hidden additional parameter called this. Passing this doesn't come for free, so making a private function static can be seen as a means of optimization.
    But it can also be seen as a means of expressing your requirements/design in your code: If that function doesn't need to refer to any member data of the class, why should it be a non-static member function?

    However, changing the type of any member function, public or private, static or not, will require all clients to recompile. If this needs to be done for a private function which those clients can never use, that's a waste of resources. Therefore, I usually move as many functions as possible from the class' private parts into an unnamed namespace in the implementation file.

提交回复
热议问题