What does this static function mean?

D

dolphin

Hi All
I read a .cpp files,find that
static void fun(void){......}

int main()
{
..........
}

What does this static function mean?Is it the same as the static
function in a class?
Thanks
 
M

Michael DOUBEZ

dolphin a ¨¦crit :
I read a .cpp files,find that
static void fun(void){......}

int main()
{
.........
}

What does this static function mean?Is it the same as the static
function in a class?

Technically, this means the name of the function won't be exported so
you can reuse it in more than one compilation unit.

From the programmer point of view, this means the function won't be
called by tis name outside the compilation unit.

An alternative is to use an unnamed namespace.

Michael
 
D

dolphin

dolphin a écrit :




Technically, this means the name of the function won't be exported so
you can reuse it in more than one compilation unit.

From the programmer point of view, this means the function won't be
called by tis name outside the compilation unit.

An alternative is to use an unnamed namespace.

Michael

Do you mean you just can use this function in that cpp file?And you
cannt use it in another cpp file?
 
M

Michael DOUBEZ

dolphin a écrit :
Do you mean you just can use this function in that cpp file?And you
cannt use it in another cpp file?

What I mean is that the name of the function is not exported in the
symbol table so you cannot link against it outside the cpp file it is
defined in.
That doesn't mean it cannot be used, you could use through is function
pointer by example. But not with its name. Try it out on an example.


main.cpp
-----------------------
extern int cu_function();

int main()
{
return cu_function();
}

cu.cpp
-----------------------
//uncomment static to see what happen
//static
int cu_function()
{
return 0;
}
-----------------------

Then
g++ main.cpp cu.cpp

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top