Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Static functions or Normal Functions

Static functions or Normal Functions

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    ugur_basak
    wrote on last edited by
    #1

    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?

    C 1 Reply Last reply
    0
    • U ugur_basak

      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?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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 ?

      U 1 Reply Last reply
      0
      • C Cedric Moonen

        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 ?

        U Offline
        U Offline
        ugur_basak
        wrote on last edited by
        #3

        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.

        J H 2 Replies Last reply
        0
        • U ugur_basak

          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.

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          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!)

          U 1 Reply Last reply
          0
          • J James R Twine

            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!)

            U Offline
            U Offline
            ugur_basak
            wrote on last edited by
            #5

            I guess its better using classic functions.

            T D 2 Replies Last reply
            0
            • U ugur_basak

              I guess its better using classic functions.

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • U ugur_basak

                I guess its better using classic functions.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                "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

                1 Reply Last reply
                0
                • U ugur_basak

                  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.

                  H Offline
                  H Offline
                  heman154
                  wrote on last edited by
                  #8

                  Look into the Singleton design pattern and see if that will work for you.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups