静态功能? [英] static function?

查看:56
本文介绍了静态功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我发现了一些功能,如


void static foo(...){

....

}


static关键字代表什么?什么是静态函数?


thanx

Hello,

I found some function like

void static foo(...){
....
}

what does the static keyword stand for? what is a static function?

thanx

推荐答案

dual0< du *** @ bluewin。 CH>潦草地写道:
dual0 <du***@bluewin.ch> scribbled the following:
你好,
我发现了一些函数,比如
void static foo(...){
...
}
static关键字代表什么?什么是静态函数?
Hello, I found some function like void static foo(...){
...
} what does the static keyword stand for? what is a static function?




强制执行静态链接。这意味着这个函数不能直接从其他翻译单元调用

,因为他们不知道它的名字

(它没有与它们相关联)。你仍然可以传递一个指向它的函数指针

并通过它调用。


-

/ - Joona Palaste (pa*****@cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ----- ----------------规则! -------- /

不,玛吉,不是阿兹台克人,奥尔梅克! Ol-mec!

- Lisa Simpson



Enforces static linkage. This means that this function cannot be called
directly from other translation units, because they won''t know its name
(it''s not linked against them). You can still pass a function pointer
pointing to it and call through that.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson


2004年4月3日08:16:27 -0800, du *** @ bluewin.ch (dual0)写道:
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
你好,

我找到了一些函数,比如

void static foo(...){
...
}
静态关键字代表什么?什么是静态功能?


当在文件范围内使用时(在源文件的顶层),静态

修饰符给出一个名称(对于一个函数或数据)内部联系;这个

基本上意味着当您将此模块与其他模块链接时,您定义的对象的名称将不会对链接器可见。因此

你可以拥有/不同/版本的foo每个模块中的功能

(翻译单元)您的程序。这对于防止跨越TU'的意外名称冲突非常方便。


在单个TU中,静态的这种使用是透明的。它真的只有
会影响/ other / TU'中的数据和函数的声明如何得到解决(或者

没有得到)。


请注意静态在块范围内使用(在定义数据的函数内)

具有不同的含义:它通常导致自动数据具有

静态存储持续时间,这意味着值保持不变(保留其值)

跨多个调用它们被定义的函数。这个

允许,例如,一个函数来跟踪它被调用的次数,或者定义一个只需要初始化的值数组

一次(在C中,这样的初始化发生在编译时。)

-leor

thanx
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you''re defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU''s.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU''s get (or
don''t get) resolved.

Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they''re defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor

thanx



-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software免费版STL错误消息解密器:
www.bdsoft.com/tools/ stlfilt.html



--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


Leor Zolman< le ** @ bdsoft.com>在消息新闻中写道:< v1 ******************************** @ 4ax.com>。 ..
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
2004年4月3日08:16:27 -0800, du *** @ bluewin.ch (dual0)写道:
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
你好,

我发现了一些像

void static foo(...){
...
}

static关键字代表什么?什么是静态函数?
当在文件范围内使用时(在源文件的顶层),静态
修饰符给出一个名称(对于函数或数据)内部链接 ;;这基本上意味着当您将此模块与其他模块链接时,您定义的对象的名称将不会对链接器可见。因此
你可以拥有/不同/版本的foo。功能在您的程序的每个模块
(翻译单元)。这对于防止跨TU的意外名称冲突很方便。

在单个TU中,静态的这种使用是透明的。它实际上只影响/ other / TU中的数据和函数的声明如何得到(或者没有得到)解决。
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you''re defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU''s.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU''s get (or
don''t get) resolved.




也许吧。但是当我编写一个包含一个翻译单元的
程序时,我会在函数上使用static。这是什么东西

这是不赞成的?在这里做其他人(comp.lang.c)吗

这个?

请注意静态在块范围内使用(在定义数据的函数内)
具有不同的含义:它通常导致自动。数据具有静态存储持续时间,这意味着值可以在对定义它们的函数的多次调用中持续存在(保留它们的值)。例如,这允许一个函数来跟踪它被调用的次数,或者定义一个只需要初始化一次的值数组(并且在C中,这样的初始化发生在编译时。)
-leor



Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?
Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they''re defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor


thanx

thanx




-

nethlek



--
nethlek


这篇关于静态功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆