Reputation: 83
If you have a definition of a function in the beginning of a .cpp file, and implement after the main function, using static in just one of them suffices its purpose?
#include <iostream>
static void foo(); //define func
int main()
{
foo();
return 0;
}
void foo() //do we need static here again or we can omit it?
{
//implementations
}
Upvotes: 1
Views: 86
Reputation: 119392
[basic.link]/3
A name having namespace scope (6.3.6) has internal linkage if it is the name of ... a variable, function or function template that is explicitly declared
static
[over.dcl]/1
Two function declarations of the same name refer to the same function if they are in the same scope and have equivalent parameter declarations (16.1). ...
Putting these together, the second declaration of foo
simply redeclares the function that has already been declared previously, and that function has already been given internal linkage. So the two declarations declare the same function, and that function has internal linkage (is "static").
Upvotes: 2