static functions?

Started by
14 comments, last by Sneftel 18 years, 7 months ago
What is a static function? I think I know what a static method is, but not a function.. So what is it? And does it automaticly make the function threadsafe or what? I've seen people wrapping entire libraries with static functions, and then call them threadsafe.. :p So, what are they, and when/why should I use them?
Advertisement
static function in what language?
A static function is identical to a static method. Method is the Java (and similar languages) way of saying it, and function is the C++ (and similar) way of saying it.

Edit: In abstract compsci terms, of course. Who can say what languages can be found in the wild for which a "static function" means something entirely different?
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Static function are for use in classes. Say you have a value you need to retrieve, but you don't have the object that member function is in. You can make it static and call name = objectsname::staticfunctionsname();
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
In C, the static declaration typically indicates that a function (or variable) is only accessible from other functions in that same file (aka translation unit). The prefix tells the compiler not to form information that the linker uses to reference functions across .obj files. This can be worked around by using the extern keyword with a prototype of a function that is declared static but that another file needs to use. For example, if function X is declared static in file A, then other functions in file A can call it, but functions in file B can't, unless at some place in file B a prototype of function X is declared using the extern keyword. This happens outside of using header files to impart function prototypes across files. For a variable, the static keyword tells the linker to provide space for the variable in the actual program file itself. The static keyword probably acts in a similar way for C++. // outside of classes that is ...

Are you certain that those libraries used the static keyword alone to ensure thread safety? Maybe other mechanisms were at work, but just weren't obvious.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Ok, so it's only for visibility then?

Yes, in C..

I can't give you an example, and no, I'm not sure that's all they do, but I know alot of projects only declare functions as static when threadsafe is enabled..
It has nothing to do with thread safety, only with scoping.
Since you are talking about static functions and methods, I've assumed you're talking about C++.

The keyword 'static' is a storage class specifier. Functions occupy a position or address on the stack just like stack-based variables. This is why you can use such things as function pointers and function indirection.

Static member functions of classes are used to produce 'per class' behaviour, as opposed to the usual 'per instance behavior'. This is important when members maintain state, and where you do not want this state to change from instance to instance, so you would make these superclass member functions 'static'. A static member function will not have a 'this' pointer, but still remains in the scope of the class and subclasses.

For general use with functions in C++, I'll quote from http://zach.chambana.net/apache-cplusplus/cplusplus/static.html

"Stroustrup (3rd ed.), page 200 says:

In C and older C++ programs, the keyword static is (confusingly) used to mean "use internal linkage" (Section B.2.3). Don't use static except inside functions (Section 7.1.2) and classes (Section 10.2.4).

Stroustrup (3rd ed.), page 819 says:

The keyword static, which usually means "statically allocated," can be used to indicate that a function or an object is local to a translation unit. For example:

// file 1:
static int glob;

// file 2:
static int glob;

This program genuinely has two integers called glob. Each glob is used exclusively by functions defined in its translation unit.

The use of static to indicate "local to translation unit" is deprecated in C++. Use unnamed namespaces instead (Section 8.2.5.1)."

Hope that helps.

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
Quote:Original post by Sneftel
It has nothing to do with thread safety, only with scoping.


Quick question...I have read about this in connection with being threadsafe, but maybe it was wrong as I am not an expert on threads. I have heard that static class functions and non-const static variables in a superclass are not threadsafe when used by subclasses. Is this true?

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
They are only as thread safe as global functions using global variables. There is no one answer, but in general they are not thread safe when used by anything, not just derived classes.

This topic is closed to new replies.

Advertisement