small question

Started by
8 comments, last by Symphonic 17 years, 10 months ago
what is the difference between a global function which is static, and another which isnt for example inline static DoSomething() { } and inline DoSomething() { } ???
Advertisement
Quote:Original post by flutey
what is the difference between a global function which is static, and another which isnt


for example

inline 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/
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
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]
Quote:Original post by Ridiculous
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.

yes, you are in fact wrong.


You can have static functions outside of classes. AP had it right about it being a visibility issue...

[edit]
beaten
moe.ron
Quote:Original post by Anonymous Poster
Quote:Original post by Ridiculous
Static 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;}


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.

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.
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!
Geordi
George D. Filiotis

This topic is closed to new replies.

Advertisement