static and non-static functions

V

Victor Bazarov

Ufit said:
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
 
E

Evan

(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() { ... }
}
 
U

Ufit

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
 
E

Evan

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() { ... }
}
 
S

Stephen Howe

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top