是否真的需要将关键字静态与函数一起使用? [英] Is there a real need to use keyword static with functions?

查看:45
本文介绍了是否真的需要将关键字静态与函数一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧


是否真的需要使用关键字静态功能,如果我们

根本没有在.h中声明他们的原型文件?


许多教科书都不会讨论这个问题和/或只讨论

静态函数的使用。我已经编程好多年了,而且我从来没有感觉到需要静态方法来实现功能。


我不是知道这是不是一件小事,因为保留字

存在。 :)

或者它存在于其他目的我无法弄清楚。


我错过了什么? :)


提前感谢您的答案

Hi everyone

Is there a real need to use keyword static with functions, if we
simply don''t declare their prototypes in .h file?

Many textbooks avoid to discuss this matter and/or discuss only the
usage of static functions. I''ve been programming for years and I never
felt a real need for the "static approach for functions".

I don''t know if this is a trivial matter since the reserved word
exists. :)
Or it exists for some other purpose I can''t figure out.

What did I have missed? :)

Thanks in advance for answers

推荐答案

lc ********** @ gmail.com 写道:

大家好


是否真的需要使用关键字静态功能,如果我们

根本就没有声明他们的原型.h文件?
Hi everyone

Is there a real need to use keyword static with functions, if we
simply don''t declare their prototypes in .h file?



是的,否则这些函数具有全局范围并污染全局

命名空间。您可能不想导出这些符号,如果使用相同的名称,您或代码的

用户将遇到链接问题

在多个来源中文件。


-

Ian Collins。

Yes, otherwise the functions have global scope and pollute the global
namespace. You may not want to export those symbols and you, or the
user of you code, will run into link problems if the same name is used
in more than one source file.

--
Ian Collins.


3月5日晚上9:10 ,lcdgoncal ... @ gmail.com写道:
On Mar 5, 9:10 pm, lcdgoncal...@gmail.com wrote:

许多教科书都避免讨论这个问题和/或只讨论

的用法静态函数。我已经编程好多年了,而且我从来没有感觉到b $ b感觉真正需要静态方法的功能。
Many textbooks avoid to discuss this matter and/or discuss only the
usage of static functions. I''ve been programming for years and I never
felt a real need for the "static approach for functions".



这很难过。


您是否拥有程序中所有功能的数据库以确保

That''s sad.

Do you have a database of all functions in your programs to make sure
that you don''t use the same function name twice?


lc ********** @ gmail.com 写道:
lc**********@gmail.com writes:

Is真的需要使用关键字静态函数,如果我们

只是不在.h文件中声明他们的原型?
Is there a real need to use keyword static with functions, if we
simply don''t declare their prototypes in .h file?



是的。如果定义了两个没有静态的函数,但是使用

同名,在不同的翻译单元中,那么结果就是

未定义的行为。使用静态可以避免这种不确定的行为。

-

C有它的问题,但是从头开始设计的语言也会有一些,

我们知道C'的问题。

--Barne Stroustrup

Yes. If two functions are defined without static, but with the
same name, in different translation units, then the result is
undefined behavior. Using static avoids this undefined behavior.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C''s problems."
--Bjarne Stroustrup


这篇关于是否真的需要将关键字静态与函数一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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