Thread: Static Functions

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Static Functions

    Hey,

    I've been trying to decide something, its for actionscript (Flash), but still easily relevant to C++.

    The purpose of a static function is so you dont have to call the function on each instance of the class; and the function is created once per class, not per instance. But in my situation I have a function that modifies an instance at a time (a normal method), so I dont need the 'call for all instances of' feature. Static variable is per class, and not per object, so of course those would save space, but what about functions?

    Basicly, I have two options:
    1) I can use a normal method for a certain instance, and I call it, and it simply modifies that instance (itself).
    2) I can make the method static, and pass the instance name as a reference, and it modifies the instance.

    I would use option 1, but I'm not sure, because option 2 yields the same results - just requires passing a reference of the instance. Since static functions are only created once per class, and normal methods are created for every object - would that mean they would take up more space?. Or does the compiler optimize so a normal method takes up as much space as a static method.

    Hopefully someone understands and knows, I'd really appreciate the help. Thanks!
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I think your understanding of static functions is wrong. IIRC the differences between a normal member function and a static one is that a static one can be called without an instance of a class, and it only has access to static data.

    Hopefully that helps, as I'm not sure I fully understand your problem...
    "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."
    -XSquared

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JaWiB
    I think your understanding of static functions is wrong. IIRC the differences between a normal member function and a static one is that a static one can be called without an instance of a class, and it only has access to static data.

    Hopefully that helps, as I'm not sure I fully understand your problem...
    I understand that. It's probably my understanding of regular methods thats wrong.

    If I have 5 methods in a class, and create 100 instances, will that increase the size more than say 1 instance? (assuming no members existed, which would increase the size). Thats basicly what it boils down to.

    Restated: If I have 1 static function, 1 regular function, and 100 instances, will that regular function increase the file size more than the static function?

    Its stated that a static function is created once per class (aka. can be called without an instance). So does that mean a regular function is created for each instance? or its created for each class, but can be called specificly by any instance of the class. If its the latter, then that would mean the size of the file doesn't increase with more instances.

    Edit: I see.. can only access static data eh. That makes sense, well that would decide my choice, but I still want to know my question above, thanks!
    Last edited by Dae; 10-23-2005 at 08:45 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Banned
    Join Date
    Oct 2005
    Posts
    4
    All functions share the same code segment and implementation. So the answer to your question is that you do not save memory by making functions static although you do save memory by making variables static.

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by frankish
    All functions share the same code segment and implementation. So the answer to your question is that you do not save memory by making functions static although you do save memory by making variables static.
    Exactly what I wanted to know, thanks man!
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

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. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  4. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  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