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.
Maybe a shorter version of the question:
Is there a proper way for a member function to use multiple objects from another class and instantiate those objects from within the class?
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().
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
class Foo{
LpFilter a{0.5f};
LpFilter b{0.5f};
public:
intoperator()(int x){
//do the thing
}
};
Foo f;
Foo g;
f(42);
g(75);
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.