Click to See Complete Forum and Search --> : static functions
usman999_1
March 5th, 2003, 10:02 AM
Hi*!
I am working on an application that sends/recives some data using winsock 2. I am doing that stuff (sending/reciving) in a class. The application should support multiple connections to the same server with different login and password. So i create multiple objects of my class. I am doing the recving of data in a worker thread. As with thread function they should be either static or global, i have made those thread functions static.
Now is it so that ONLY ONE instance of the static thread function exist for all of my classes objects????????? The application works fine but i tried with a LOCAL static var and that was same in all the objects of the class, i want to know is it the same in case of functions???
Thanks for your time.
Regards,
Usman.
dude_1967
March 5th, 2003, 10:57 AM
USman,
A static member function of a class has ONE instance, no matter how many class objects have been instantiated. The static function has one single stack and context. Local static variables within the static class member function are unique and exist once. This is also true for local register or automatic variables or objects.
Sincerely, Chris.
:)
gstercken
March 5th, 2003, 11:25 AM
Originally posted by dude_1967
A static member function of a class has ONE instance, no matter how many class objects have been instantiated. The static function has one single stack and context. Local static variables within the static class member function are unique and exist once. This is also true for local register or automatic variables or objects.
Sorry, Chris, but this is not correct. What you are saying applies to static member variables: They exist only once for the entire class, they are shared by different objects of the same class (in fact, they are actually global variables, with visibility limited to the class).
As for static member functions, the thing is completely different: For functions, it makes no sense to speak of multiple or single instances, as the code is always present only once (for static as well as for nonstatic functions). On the other hand, a new stack frame is created for every function call, again, independently from whether a function is static or not. And the automatic variables inside a static member function are not static.
The point about static member functions is that they exist independently from an object of that class. Thats why you can write static factory functions which create instances of that class. Therefore, static member functions have no this-pointer (you can't use the this keyword inside such a function, and you can't access the classes' nonstatic members (as they require an instance of that class).
The reason why thread functions must be static is that they are callback functions and are therefore expected to have a specific signature (number and type of parameters), usually defined by a C function prototype. Using a nonstatic member function would add the (implicit) this-pointer, and the function's signature would differ from the expected one.
You can look at a static member function just as at a globally defined function with visibility limited to the class it is defined in.
dude_1967
March 6th, 2003, 09:05 AM
gstercken,
Thanks for clearing this up. Indeed, each call of a static member function will create a new call stack and the register and automatic variables will be created again and newly and independently for each and every call. This can have interesting side-effects for multithreaded programs for which different threads call the same static member function.
Local static variables within a static member function have, I think, one and only one instantiation.
Thanks again!
Sincerely, Chris.
:)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.