What does it mean when the function in global namespace is declared as static C++? [duplicate]
Possible Duplicate:
What is a “static” function?
I have seen a function in a global namespace that is declared like this:
static int function_name(int a, double* p, int c, float * u)
{
//do something with these arguments
}
What the static keyword means here?
EDIT: Now when I know what is for static, please explain what advantage gives the restriction of a function to be visible in a file only where it is declared? I mean why I should restrict my function visibility, what 开发者_运维知识库it gives to me?
The return value is not static int. The function is a static function returning an int.
See what is a static funciton
You would use a static function with the scope in the compilation unit when you really want the function with that name to be known only inside that compilation unit. A class or function that is not in that scope cannot accidentally call the function. (I'd have put this in a comment but I don't have privileges for that yet)
It isn't the return value that's static, it's the function. It means that the function is visible within that compilation unit (= file, approximately) but not elsewhere.
As already mentioned, static refers to the whole function, not its return type. I want to add that the use of the static keyword in this context is deprecated in C++. Anonymous namespaces are the better way to go.
namespace
{
int function_name(int a, double* p, int c, float * u)
}
精彩评论