Skip to main content
gamedev.net
🔒 Locked

static functions?

Started by i8degrees Apr 12, 2002 at 11:56 PM 4 replies 200+ views
Original Post
i8degrees
i8degrees
What does a static function do? static int add_number(5, 5); Would it be the same thing as an inline function?
"I am governed by none other than the Laws of the Universe."
elis-cool
elis-cool
If it is in the .cpp file it will make it be able to only be used in that file and not in others, but if its a member function of a class that means you can act on it without an object of the class eg ClassName::add_number(5,5); instead of Object.add_number(5,5) etc...

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
fallenang3l
fallenang3l
Prefixing a function header with keyword static makes that function have static internal linkage, which means it is only known to the file in which it is declared. It also means you can create another function of the same name and signature in another file of your program and the two functions won''t collide.
Even though inline functions have static linkage too, the two aren''t the same. When you call an inline function from your progam, the call gets replaced with the actual function body so there''s no function call overhead. When you call a static function, it''s just like calling a regular function.
elis-cool
elis-cool
Wouldcha look at that, we both posted at the same time!

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
cMADsc
cMADsc
To add a little bit more to what has already been posted,
static functions donot have a "this" pointer, can not be made
virtual, can not have another function by the same name, can only access static data. You can make your static variables private and use the static function to access them. Also, remember to redeclare your static data outside of the class. I would suggest if you want to hide a function from other files instead of making it static use an unamed namespace instead.





-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.