Is there a way to use multiple objects from one class within another class?

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.
I'm trying to refactor a function as a class so I can use multiple instances.
Multiple instances of what?

The objects are instantiated with parameters using the static keyword
Do you mean that the objects are static locals within the function? I.e.
1
2
3
void function(){
    static int static_local = 0;
}
but I think it would only make it more confusing

Think again.

Multiple instances of what?


reusing the function which has both static variables and uses objects from a class.


The objects are instantiated with parameters using the static keyword
Do you mean that the objects are static locals within the function? I.e.

1
2
3
void function(){
    static int static_local = 0;
}



No. more like
1
2
3
4
int MyFunction(int x){
	static ClassA Object1 (.5f);
	static ClassA Object2 (.5f);
}


I'm trying to avoid:
having the constructor and parameters far away from where they are used.
globals.
writing the same code more than once.


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class LpFilter {
private:
  float old_f;
  float coef;
public:
  LpFilter(float c) { coef = c; }

  float Lpf(float sf) {
    float lpf_f = old_f + ((sf - old_f) * coef);
    old_f = lpf_f;
    return lpf_f;
  }
  void Coef(float c) { coef = c; }
};
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:
    int operator()(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.
Last edited on
Topic archived. No new replies allowed.