# small question

This topic is 4403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

what is the difference between a global function which is static, and another which isnt for example inline static DoSomething() { } and inline DoSomething() { } ???

##### Share on other sites
Quote:
 Original post by fluteywhat is the difference between a global function which is static, and another which isntfor exampleinline static DoSomething(){}and inline DoSomething(){}???

the difference would be (in)visibility outside the compilation unit, that is static functions are a way to avoid namespace pollution, which can also significantly reduce compilation and link times due to a reduced symbol lookup table. So, if you don't need to access a function outside the unit it's declared in, make it static.
http://www.cplusplus.com/doc/tutorial/

##### Share on other sites
By the way, googling would have brought up a number of directs hits, i.e. the first one being:
http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/static.htm

##### Share on other sites
Static fuctions are only used in classes (I may be wrong). You can not declare a static function outside of a class (I tried). The main difference between a static function and a nonstatic function is that the static function lacks a this pointer (but that doesn't keep you from passing one in). Static functions only have access other members of the class it belongs to.

By the way, the functions you gave are missing a return type. [smile]

##### Share on other sites
Quote:
 Original post by RidiculousStatic fuctions are only used in classes (I may be wrong). You can not declare a static function outside of a class (I tried). The main difference between a static function and a nonstatic function is that the static function lacks a this pointer (but that doesn't keep you from passing one in). Static functions only have access other members of the class it belongs to.

yes, you are in fact wrong.

##### Share on other sites
You can have static functions outside of classes. AP had it right about it being a visibility issue...

beaten

##### Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
 Original post by RidiculousStatic fuctions are only used in classes (I may be wrong). You can not declare a static function outside of a class (I tried).

yes, you are in fact wrong.

What exactly did you "try" (source code)?

// static.cxxstatic void HeIsWrong(){//nop}int main(){  HeIsWrong();  return 0;}

##### Share on other sites
In C:
static functions may be declared outside of classes, doing so hides the function definition from other translation units

In C++:
The use of the static keyword to denote a translation unit local function is depracated. Use an anonymous namespace instead.

##### Share on other sites
Slight correction: C has no classes. So obviously static functions can not be declared inside classes.

In C++, the static keyword is there to mark the member common to all class instances.

##### Share on other sites
Funnily enough, the example the OP gave is nearly a non-example, because inlined functions are not compiled independently and are therefore not found in the symbol table for the object file anyway. Of course, many compilers will provide a compiled version for debug mode so that the debugger can step through the inlined function, but this is not required.

inline static foo bar();

would be equivalent to

inline foo bar();

but

static foo bar();

is not exported to the symbol table, whereas

foo bar();

is.

Of course, the behaviour of the static keyword inside a class declaration is totally different, thankyou Bjarne!

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999484
×