什么是使用私有静态成员函数? [英] What is the use of private static member functions?
问题描述
我在查看请求解析器
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
? :
class request_parser
{
...
private:
static bool is_char(int c);
...
};
它用于函数 consume 这不是一个静态函数:
It is used in the function consume which is not a static function:
boost::tribool request_parser::consume(request& req, char input)
{
switch (state_)
{
case method_start:
if (!is_char(input) || is_ctl(input) || is_tspecial(input))
{
return false;
}
...
只有成员函数可以调用 is_char()
,没有静态成员函数调用 is_char()
。所以有这些函数是静态的吗?
Only member functions can call is_char()
and no static member function is calling is_char()
. So is there a reason why these functions are static?
推荐答案
这个函数可以很容易地做独立,因为它不需要该类的对象在其中操作。使函数成为类的静态成员而不是自由函数具有两个优点:
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:
- 它赋予函数访问私有和受保护成员
- 它以类似于命名空间的方式将该函数与类关联起来。
在这种情况下,只显示第二点。
In this case it appears only the second point applies.
这篇关于什么是使用私有静态成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!