Thread: Static functions.... why?

  1. #1
    Registered User
    Join Date
    Jun 2006

    Static functions.... why?

    Hi gang,

    OK, so I know about static functions and how to declare and implement them, but reviewing the list of what they are used for (pasted below), I don't see any advantage in using them over a normal non-class function. I know one of you here pros can break this down to me.



    The differences between a static member function and non-static member functions are as follows.

    A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.

    A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.

    A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual

    A static member function cannot have access to the 'this' pointer of the class.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Any data you assign to a static member of a class is accessible via any object instance of that class.

    It only occupies memory once for any number of instances

    It's more of a usage thing than a pro/con thing. It's like being able to specify a class specific global
    we are one

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    I've never had a use for them. Then again, I've only been programming for two years.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Seattle, WA
    Look up the "singleton pattern" for one common use of static member functions.

    Sometimes you need to use a static function for use as a callback (for example, when using the Win32 API).

    That's just a couple examples. In general, you use them any time the function only needs to access data that is common across all instances of the class. Also, I've placed static functions in a class where they could just as well have been non-member functions, but when they are specifically used in relation to the class (you might also use a namespace in this case).
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."

  5. #5
    Registered User
    Join Date
    Jun 2006
    OK, thanks, so it's probably best to just know how to do this for the time being. I'll see if an opportunity to use them presents itself.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  5. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM