C++ 私有静态成员函数有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6445927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of private static member functions?
提问by rve
I was looking at the request parserfrom the boost::asio example and I was wondering why the private member functions like is_char()
are static
? :
我正在查看boost::asio 示例中的请求解析器,我想知道为什么私有成员函数像is_char()
are static
?:
class request_parser
{
...
private:
static bool is_char(int c);
...
};
It is used in the function consumewhich 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;
}
...
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?
只有成员函数可以调用is_char()
,没有静态成员函数在调用is_char()
。那么这些函数是静态的有什么原因吗?
回答by Mark Ransom
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:
这个函数很容易成为独立的,因为它不需要类的对象来操作。使函数成为类的静态成员而不是自由函数有两个优点:
- 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;
- It associates the function with the class in a similar way to a namespace.
- 如果对象是静态的或传递给函数,则它允许函数访问类的任何对象的私有成员和受保护成员;
- 它以类似于命名空间的方式将函数与类相关联。
In this case it appears only the second point applies.
在这种情况下,似乎只有第二点适用。
回答by sbi
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?
非static
成员函数有一个名为 的隐藏附加参数this
。传递 this 不是免费的,所以制作一个private
函数static
可以看作是一种优化的手段。
但它也可以看作是在代码中表达您的需求/设计的一种方式:如果该函数不需要引用类的任何成员数据,为什么它应该是非static
成员函数?
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.
但是,更改任何成员函数的类型,public
或private
,static
或不,将需要所有客户端重新编译。如果需要为private
那些客户端永远无法使用的功能执行此操作,那就是浪费资源。因此,我通常将尽可能多的函数从类的私有部分移动到实现文件中的未命名命名空间中。
回答by dolphy
For this specific example, the choice for a static is_char()
is most likely a documentation one. The intent is to impress upon you that the is_char()
method is not contrained to a specific instance of the class, but the functionality is specific to the class itself.
对于这个特定示例,a 的选择static is_char()
很可能是文档选项。目的是让您印象深刻,该is_char()
方法不限于类的特定实例,但功能特定于类本身。
In other words, by making it static
they are saying that is_char()
is a utility function of sorts...one which can be used irrespective of the state of a given instance. By making it private
, they are saying that you (as a client) should not try to use it. It either does not do what you think it does, or is implemented in a very constrained, controlled way.
换句话说,通过制作它,static
他们说这is_char()
是一种效用函数......无论给定实例的状态如何都可以使用它。通过制作它private
,他们是在说您(作为客户)不应该尝试使用它。它要么没有做你认为它做的事情,要么以一种非常受约束、受控的方式实现。
@Mark Ransom's answer brings up a good point for the practical use of a private static member function. Specifically, that member function has access to private and protected members of either a static object or a passed instance of an instantiated object.
@Mark Ransom 的回答为私有静态成员函数的实际使用提出了一个很好的观点。具体来说,该成员函数可以访问静态对象或实例化对象的传递实例的私有成员和受保护成员。
One common application of this is to abstract a pthread implementation in somewhat of an object oriented way. Your thread function must be static, but declaring it private limits the accessibility of that function to the class (to all but the most determined). The thread can be passed an instance of the class it's being "hidden" in, and now has access to perform logic using the object's member data.
它的一个常见应用是以某种面向对象的方式抽象 pthread 实现。您的线程函数必须是静态的,但是将其声明为私有会限制该函数对该类的可访问性(除了最确定的之外)。可以向线程传递它“隐藏”在其中的类的实例,并且现在可以使用对象的成员数据访问执行逻辑。
Simplistic Example:
简单示例:
[MyWorkerClass.h]
...
public:
bool createThread();
private:
int getThisObjectsData();
pthread_t myThreadId_;
static void* myThread( void *arg );
...
[MyWorkerClass.cpp]
...
bool MyWorkerClass::createThread()
{
...
int result = pthread_create(myThreadId_,
NULL,
myThread),
this);
...
}
/*static*/ void* MyWorkerClass::myThread( void *arg )
{
MyWorkerClass* thisObj = (MyWorkerClass*)(arg);
int someData = thisObj->getThisObjectsData();
}
...
回答by Frerich Raabe
It's static, since it doesn't require access to any member variables of request_parser
objects. Hence, making it static decouples the function since it reduces the amount of state which the function can access.
它是静态的,因为它不需要访问request_parser
对象的任何成员变量。因此,使其静态解耦函数,因为它减少了函数可以访问的状态量。
For what it's worth, it would have been even better if this function wasn't part of the request_parser
class at all - instead, it should have been (possibly in a namespace) a free function in the .cpp
file.
就其价值而言,如果这个函数根本不是request_parser
类的一部分,那就更好了——相反,它应该是(可能在命名空间中).cpp
文件中的一个免费函数。
回答by Armen Tsirunyan
The point isn't whereit is used. The question is whatit uses. If its definition doesn't use any nonstatic members, I would make the function static, according to the same principle that I wouldn't pass a redundant parameter to any function (unless they were to be used in overload resulion)
重点不在于它在哪里使用。现在的问题是什么,它使用。如果它的定义不使用任何非静态成员,我会将函数设为静态,根据同样的原则,我不会将冗余参数传递给任何函数(除非它们被用于重载结果)