What is the use of private static member functions?

前端 未结 5 2139
一生所求
一生所求 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:29

    This function could easily have been made freestanding, since it doesn't require an object of the class to operate within. Making a function a static member of a class rather than a free function gives two advantages:

    1. It gives the function access to private and protected members of any object of the class, if the object is static or is passed to the function;
    2. It associates the function with the class in a similar way to a namespace.

    In this case it appears only the second point applies.

提交回复
热议问题