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