什么是"静态"功能? [英] What is a "static" function?
问题描述
The question was about plain c functions, not c++ static
methods, as clarified in comments.
好吧,我明白了一个静态
变量是什么,但什么是静态
功能?
Ok, I understand what a static
variable is, but what is a static
function?
和为什么它,如果我宣布一个函数,假设无效print_matrix
,在假设 AC
( WITHOUT 啊
),并包括AC
- 我得到print_matrix @@ ... )在A.OBJ已经定义了
,但如果我把它声明为静态无效print_matrix
然后再编译?
And why is it that if I declare a function, let's say void print_matrix
, in let's say a.c
(WITHOUT a.h
) and include "a.c"
- I get "print_matrix@@....) already defined in a.obj"
, BUT if I declare it as static void print_matrix
then it compiles?
更新只是为了澄清一些事情 - 我知道,包括 .C
是坏,正如你们许多人指出的那样,我只是做了在的main.cpp
暂时清除空间,直到我有一个更好的想法如何把所有这些功能集成到正确的 .HPP
和 .C
。只是一个暂时的,快速的解决方案。
UPDATE Just to clear things up - I know that including .c
is bad, as many of you pointed out, I just do it to temporarily clear space in main.cpp
until I have a better idea of how to group all those functions into proper .hpp
and .c
. Just a temporary, quick solution.
推荐答案
静态
函数是只对在同一个文件等功能可见的功能(更多precisely的同样翻译单元 的)。
static
functions are functions that are only visible to other functions in the same file (more precisely the same translation unit).
修改:对于那些谁想到,那的问题,笔者意味着一个类方法:由于这个问题被标记 C
他意味着一个普通的老C函数。对于(C ++ / Java的/ ...)类的方法,静态
表示,这种方法可以在类本身被调用,没有必要的类的实例。
EDIT: For those who thought, that the author of the questions meant a 'class method': As the question is tagged C
he means a plain old C function. For (C++/Java/...) class methods, static
means that this method can be called on the class itself, no instance of that class necessary.
这篇关于什么是"静态"功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!