0

Can You help rewrite this class in a more verbose way ie(without constructor initialization list and the float operator overload) or explain how it is working

class HigPassFilter { public: HigPassFilter(float reduced_frequency) : alpha(1 - exp(-2 * PI*reduced_frequency)), y(0) {} float operator()(float x) { y += alpha * (x - y); return x - y; } int myfunc(bool x) { return 1; } private: float alpha, y; }; 
7
  • 2
    Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist. Commented Nov 23, 2018 at 9:29
  • 3
    If you have troubles with understanding initialization list, you may want to have a look at our list of good books to learn C++. Commented Nov 23, 2018 at 9:29
  • "without constructor initialization list" For instance, instead of y(0), you put y = 0; in the constructor's body. But honestly, I don't see why you would want to do that. Commented Nov 23, 2018 at 9:30
  • Why would you want to do this? Commented Nov 23, 2018 at 9:30
  • Why would you want to drop initalization list? The request to remove operator is unclear. 1. It's not a "float operator" 2. Where would this functionality go? It's really hard to tell what your problem is here... One thing you could do is to wrap calculation for initializing alpha into a private function. Commented Nov 23, 2018 at 9:31

1 Answer 1

2

I will explain how this is working:

class HigPassFilter { public: // Constructor of the class, with one parameter. HigPassFilter(float reduced_frequency) // initializer list initializes both data members of the class, // 'alpha' will be set to the result of '1 - exp(-2 * PI*reduced_frequency)' // and 'y' will be set to 0 : alpha(1 - exp(-2 * PI*reduced_frequency)), y(0) // the body of the constructor is empty (good practice) {} // An overload of operator(), which performs a mathematical operation. // It will increment 'y' by 'alpha * (x - y)' and // return the difference of 'x' and 'y' float operator()(float x) { y += alpha * (x - y); return x - y; } // a simple function that returns always 1 and // will not used its parameter, causing an unused warning (bad practice) int myfunc(bool x) { return 1; } private: // private data members float alpha, y; }; 

Read more in What is this weird colon-member (“ : ”) syntax in the constructor?. Initializer lists are a very important feature of C++, so I suggest you spend some time learning about them. Most of the times, you will initialize your data members in the initializer list-that's why this feature exists anyway.

Further reading: Why override operator()?

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, understand constructor syntax now, and the operator is explicitly converting the input parameter to a float, and overrides the class constructor with a signature of (float), with y += alpha * (x - y); return x - y; ?
@davidgangy you are welcome. If this answer helped, don't forget to accept it. As of your comment, parameter casting happens just like it would happen in any usual function you are using. It will overload the operator(), not the constructor! Read about overload vs override.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.