By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

static and non-static functions

P: n/a
What is the difference between those two. Is there gonna
be some difference in app execution for static functions?
What difference does it make?

UF
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ufit wrote:
What is the difference between those two. Is there gonna
be some difference in app execution for static functions?
What difference does it make?


Static functions do not require (nor do they have, internally)
an instance of the class to be called. Non-static, vice versa,
do require and provide (through 'this' pointer) an instance of
the class for which they are called.

I am not sure how to answer the second question. The third
question is too vague.

V
Jul 23 '05 #2

P: n/a
(Hopefully this won't show up twice; if it does, I apologize. I'm
reasonably sure Google dropped my first post though, so here goes:)
What is the difference between those two. Is there gonna
be some difference in app execution for static functions?
What difference does it make?


Victor answered your questions for member functions in classes. (If
not, consult Google or your C++ book; anything that talks about classes
should talk about this forn of static-ness.) However, there can be free
(non-member) functions that are static too, and static in this context
means something entirely different.

Normally you can use a function defined in another compilation unit by
using the extern keyword:

File1.cpp
int foo() { ... }

File2.cpp
extern int foo() { ... }

int main() { foo(); }
However, if you have functions that you don't want to be able have this
done to (sorta similar to the idea of private member functions), you
can make them static:

File1.cpp
static int foo() { .. }

Now a linker error will be generated because extern int foo() can't
pull it in.

This use has been superceded in C++ by the unnamed namespace. File1
(the second version) should be refactored as:

File1.cpp
namespace
{
int foo() { ... }
}

Jul 23 '05 #3

P: n/a
Victor Barzarov answered your question for functions are class members,
but there can also be non-member static functions. (If you have other
questions consult Google or your favorite C++ book.)

These, along with any global static variables, restrict the scope of
the function to just that file. In other words, other compilation units
cannot use extern to get to those functions.

File1.cpp:
static void foo() { ... }

File2.cpp
void bar() { ... }

File3.cpp
extern void foo() { ... } // illegal
extern void bar() { ... } //okay
This use has been superceded in C++ by the unnamed namespace. File1.cpp
should be refactored into:

File1.cpp
namespace
{
void foo() { ... }
}

Jul 23 '05 #4

P: n/a
I assume that you are talking about standalone functions and not class
member functions.
Is there gonna be some difference in app execution for static functions?


Can be. One compiler I know of for Intel "16-bit large code models" will do
a certain optimisation knowing that a function is static to the translation
unit. That makes it fractionally faster.

Stephen Howe
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.