I'm trying to refactor a function as a class so I can use multiple instances.
The function currently uses four objects of the same class. The objects are instantiated with parameters using the static keyword so that their declaration is close to where they are used.
This works fine as a function, but doesn't when when refactored into a class. Is there a normal way to do this? I've tried writing some sample code to show what I'm trying to do, but I think it would only make it more confusing.
You haven't explained why Object1 and Object2 need to be static.
Typical usage would be that Object1 and Object2 are not static unless they need to retain their state between calls to MyFunction().
Here is the class I want to use multiple instances of within another class. It's a moving average filter so retaining data is necessary. The objects are never destroyed and run for the life of the program on an embedded ARM processor. This class is also used in several other places in the main loop.
The proper way to objectify a function like that would be
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//do the thing
Note that now f and g have independent states, and that the lifetimes of Foo::a and Foo::b are now tied to the lifetime of the owning object, unlike Object1 and Object2 of MyFunction() which live and maintain their state for the entire duration of the program.