静态函数有什么用处?
我不明白所有这些关键字。特别是这个static
。如果有一个例子来说明它的重要性以及它如何使用,那就太好了。
I don't get all these keywords. Specially this one static
. An example of how important it is and how it used would be wonderful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将成员函数设置为
static
允许您在不创建类对象的情况下调用该函数。查看
单例模式
以了解有关它如何提供帮助的更多信息。Making a member function
static
allows you to call the function without creating the class object.Check out the
Singleton pattern
for more information on how it can be helpful.静态成员函数就像常规函数一样。
输出:
您可以像常规函数一样对待它们,这意味着您可以将它们传递给仅接受常规函数作为参数的其他函数,如下所示:
输出:
static member functions are just like regular functions.
Output:
You can treat them just like regular functions, that means, you can pass them to other functions which accept only regular function as argument, like this:
Output:
static 关键字有多种用途,根据您使用它的位置,它会执行不同的操作。
http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx
There are multiple uses for the static keyword, it does different things based on where you use it.
http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx
静态类和类成员用于创建无需创建类实例即可访问的数据和函数。优点是不需要实例化类即可使用方法或属性。
何时使用静态类的一个示例可能是实用程序函数,例如转换器(例如华氏温度到摄氏温度)。无论任何对象或数据如何,这种类型的函数都不会改变。
在 C# 中,您可以像这样调用静态方法:
以下是静态类和方法的定义方式:
Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. The advantage is that you don't need to instantiate the class to use methods or properties.
An example of when to use a static class might be for utility functions such as converters (e.g. Fahrenheit to Celcius). This type function doesn't change irregardless of any object or data.
In C# you can call a static method like this:
Here is how the static class and methods are defined:
静态函数有两种类型:类/结构成员和非成员。我猜你想知道前者(因为它更令人困惑)...
静态成员函数
如果我们对比四个函数:
并给出:
我们可以写:
x.non_static_member_f (arg)
- x 是现有的X
对象实例 - 可通过this
指针进行访问。该函数可以完全访问所有private
/protected
/public
static
/non-staticX
的 code> 成员,用于对x
和arg
进行操作。X::static_member_f(arg)
可以使用单个X
参数调用 - 如果函数未指定X
参数,那么可以在没有现有X
对象的情况下调用它。它可以完全访问X
的所有private
/protected
/public
static
,并且可以访问arg
上的任何非static
成员。non_member_friend_f(arg)
与X::static_member_f(arg)
具有相同的访问权限,但不在X
内部(即您不需要使用X::
前缀调用它,Koenig 查找的解析方式不同)。non_member_f(arg)
只能访问arg的public
成员,没有特殊权限。为了完整性:静态非成员函数与非静态函数的不同之处在于具有内部链接,这意味着它们不能从其他翻译单元调用,但不会与这些翻译单元中的任何同名函数发生冲突。
There are two types of static functions: class/struct member, and non-member. I guess you're wondering about the former (as it's more confusing)...
static member functions
If we contrast four functions:
And given:
We can write:
x.non_static_member_f(arg)
- x is an existingX
object instance - made accessible via thethis
pointer. The function has full access to allprivate
/protected
/public
static
/non-static
members ofX
for operations onx
andarg
.X::static_member_f(arg)
can be invoked with a singleX
argument - if the function didn't specify anX
argument, then it could be called with no existingX
objects. It has full access to allprivate
/protected
/public
static
ofX
, and can access any non-static
members onarg
.non_member_friend_f(arg)
has the same access asX::static_member_f(arg)
, but is not scoped insideX
(i.e. you don't need to call it with theX::
prefix, Koenig lookup resolves differently).non_member_f(arg)
can only access thepublic
members of arg, and has no special privileges.For completeness: static non-member functions differ from non-static in having internal linkage, which means they're not callable from other translation units but won't clash with any same-named function in those translation units.
静态函数在实现所谓的命名构造函数时非常有用。
想象一个 Point 类,它可以从直角坐标(X/Y)或极坐标(半径和角度)构造:
使用创建 Point 的静态函数可以很好地解决这个问题代码>对象;这些函数被称为
命名构造函数
,因为它们的行为类似于构造函数(它们生成一个新对象),但它们可以有一个描述性名称:类的客户端现在可以使用这些命名构造函数来创建可读的、明确的代码:
此外,命名构造函数可用于确保类的对象始终分配有
new
(以便您知道您始终可以对它们调用delete)。有关详细信息,请参阅常见问题解答 [16.21]。Static functions are very useful when implementing so-called Named Constructors.
Imagine a
Point
class which can be either constructed from rectangular coordinates (X/Y) or polar coordinates (radius and angle):This can be solved very nicely using static functions which create
Point
objects; such functions are callednamed constructors
since they act like a constructor (they produce a new object) but they can have a descriptive name:Clients of the class can now use these named constructors to create readable, unambiguous code:
Furthremore, named constructors can be used to make sure that objects of a class are always allocated with
new
(so that you know that you can always call delete on them). See FAQ [16.21] for more information.静态类成员函数很有用:
静态函数很有用:
1.避免编译时重复代码重定义。将为包含其中的每个 cpp 单元重新定义一个静态函数。
而且我认为现在还有大量其他有用的情况我不记得了:-)
static class member functions are useful:
static functions are useful:
1. to avoid duplicate code redefinition while compiling. A static function will be redefined for each cpp unit it is included in.
And I think there are tons of other useful cases I don't remember of right now :-)
martona 的答案很好地概述了
静态。关于静态成员,我认为 Tony 很好地涵盖了它。
当涉及到成员函数时,我使用的心理模型是考虑如何在“C”中对它们进行建模:
可能实现为:
所有函数都是成员,因此对私有成员具有适当的“访问权”。非静态成员有一个“this”指针,它提供对特定实例成员的访问。静态成员没有这样的指针,这就是为什么我们无法访问任何非静态成员。
The answer from martona is a good overview of
static
. Relating to static members, I think Tony covers it pretty well.The mental model I use when it comes to member functions is to consider how they might be modeled in 'C':
Might be implemented as:
All the funcitons are members and so have appropriate 'access' to private members. The non static members have a 'this' pointer, and it is that which provides the access to a specific instances members. The static member doesn't have such a pointer and this is why we cannot access any non static members.