Thread: Function question..

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Function question..

    Hi.. i want to make a function in order to store variable's values.
    e.g if i have a value_1=0 and value_1+=1, in the function, i want the second time the function run, the value=1 is the init value...
    Ready..Steady..Doom!!!

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    As far as I know a functor is just any normal class that declares operator().

    though for your needs a function with static local variables might be enough (unless the god of good OOP frown on them of course).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Static variables are indeed what you probably need here; the question, though, is why? If the function is really standing alone, statics might be acceptable, but that depends heavily on the case. For example, statics are never thread safe. Also, if the variable in question has any relation to an object, it might be a better idea to make it a member of said object. Perhaps a static member.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Why did I read function as functor?

    Anyway if the function is not part of any object a functor might be a more robust solution than static variables.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    directed at sigfriedmcwild:

    Static variables aren't frowned upon, but they are in a sense like gobal variables, you must first ask yourself: "Do I really have no other way of doing this?" Then you must ask yourself: "should I just make this variable a global?" And finally: "If I have multiple threads that may be using this function, will it cause a race conditon or any sort of undireable results?"

    Keeping those questions in mind may keep you from making sloppy code that could have been done another way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM