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;
};
gsamaras
  • 71,951
  • 46
  • 188
  • 305
davidgangy
  • 17
  • 1
  • 2
    Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Nov 23 '18 at 09: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++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Yksisarvinen Nov 23 '18 at 09: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. – Blaze Nov 23 '18 at 09:30
  • Why would you want to do this? – Robert Andrzejuk Nov 23 '18 at 09: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. – luk32 Nov 23 '18 at 09:31
  • Why do you want the class to NOT use an initialiser list? Using an initialiser list is, in many ways, better technique than alternatives. – Peter Nov 23 '18 at 09:34
  • Hey, everyone, the question is not about removing initializer list. OP simply doesn't understand the code they got somewhere and needs help in that. They don't understand initializer list, so they would like to see the code without initializer list. – Yksisarvinen Nov 23 '18 at 09:44

1 Answers1

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()?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 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 Nov 23 '18 at 11:59
  • @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](https://stackoverflow.com/questions/11912022/differentiate-between-function-overloading-and-function-overriding). – gsamaras Nov 23 '18 at 12:38