Static functions or Normal Functions
-
I have class, all of my database functions are in it. I wonder, using all these functions as static and calling them staticly or make it as standart object and declare it in document class and call the functions by GetDocument()->.... which one is better? What are the advantages of static funtions?
-
I have class, all of my database functions are in it. I wonder, using all these functions as static and calling them staticly or make it as standart object and declare it in document class and call the functions by GetDocument()->.... which one is better? What are the advantages of static funtions?
Static functions in a class means that they are 'not bound to a particular instance of the class'. That means you can call this function directly like that:
CMyClass::StatFunc();
In fact the function is 'shared' across all instances of the class. But that also means that inside this function, you cannot make call to other non-static function of the class or use non-static member variables of the class. So I really don't understand what you try to achieve by doing this ? :doh: Why don't use a normal class for doing that ? -
Static functions in a class means that they are 'not bound to a particular instance of the class'. That means you can call this function directly like that:
CMyClass::StatFunc();
In fact the function is 'shared' across all instances of the class. But that also means that inside this function, you cannot make call to other non-static function of the class or use non-static member variables of the class. So I really don't understand what you try to achieve by doing this ? :doh: Why don't use a normal class for doing that ?Just i thought if there'll be a large class full of functions (all database part take place in this func) may use a lot memory, so i thought that may be it is better to call them staticly so there wont be an object in memory everytime.
-
Just i thought if there'll be a large class full of functions (all database part take place in this func) may use a lot memory, so i thought that may be it is better to call them staticly so there wont be an object in memory everytime.
If the function itself is not written well and it abuses memory, it is going to do so regardless of being a normal class member or a static member. If you end up having static data members used by the functions, make sure you protect them against concurrent access if you have multiple threads running. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
If the function itself is not written well and it abuses memory, it is going to do so regardless of being a normal class member or a static member. If you end up having static data members used by the functions, make sure you protect them against concurrent access if you have multiple threads running. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)I guess its better using classic functions.
-
I guess its better using classic functions.
Why is it better to use classic functions? If you don't need "this", don't use it. Make the functions static, that is what they are. If they need to call non-static methods, then your class isn't well defined. If you have static variables in these static methods, then your class isn't well defined. If you don't need "this", make the routine static. If you do need "this", make it a thiscall. Use the right thing in the right place. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
I guess its better using classic functions.
"Better" is a subjective term. Static and non-static methods each have their place. It all depends on the nature of the problem.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Just i thought if there'll be a large class full of functions (all database part take place in this func) may use a lot memory, so i thought that may be it is better to call them staticly so there wont be an object in memory everytime.