Sign in to follow this  
flutey

small question

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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/

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest 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). 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 this post


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

[edit]
beaten

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.cxx

static void HeIsWrong()
{
//nop
}

int main()
{
HeIsWrong();
return 0;
}




Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this