Original Post
🔒 Locked
static functions?
Advertisement
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
CEO Plunder Studios
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.
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.
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."
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."
The Static Keyword - Part I
The Static Keyword - Part II
Read.
[Edit: Typo.]
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
[edited by - Oluseyi on April 13, 2002 2:31:38 PM]
The Static Keyword - Part II
Read.
[Edit: Typo.]
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
[edited by - Oluseyi on April 13, 2002 2:31:38 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement