Deconstruct c++ Class operator overload and constructor initialization list












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;
};









share|improve this question




















  • 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.

    – Some programmer dude
    Nov 23 '18 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++.

    – Yksisarvinen
    Nov 23 '18 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.

    – Blaze
    Nov 23 '18 at 9:30













  • Why would you want to do this?

    – Robert Andrzejuk
    Nov 23 '18 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.

    – luk32
    Nov 23 '18 at 9:31


















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;
};









share|improve this question




















  • 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.

    – Some programmer dude
    Nov 23 '18 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++.

    – Yksisarvinen
    Nov 23 '18 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.

    – Blaze
    Nov 23 '18 at 9:30













  • Why would you want to do this?

    – Robert Andrzejuk
    Nov 23 '18 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.

    – luk32
    Nov 23 '18 at 9:31
















0












0








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;
};









share|improve this question
















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;
};






c++ oop constructor operators






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:33









gsamaras

52.1k25107193




52.1k25107193










asked Nov 23 '18 at 9:26









davidgangydavidgangy

141




141








  • 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.

    – Some programmer dude
    Nov 23 '18 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++.

    – Yksisarvinen
    Nov 23 '18 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.

    – Blaze
    Nov 23 '18 at 9:30













  • Why would you want to do this?

    – Robert Andrzejuk
    Nov 23 '18 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.

    – luk32
    Nov 23 '18 at 9:31
















  • 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.

    – Some programmer dude
    Nov 23 '18 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++.

    – Yksisarvinen
    Nov 23 '18 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.

    – Blaze
    Nov 23 '18 at 9:30













  • Why would you want to do this?

    – Robert Andrzejuk
    Nov 23 '18 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.

    – luk32
    Nov 23 '18 at 9:31










2




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.

– Some programmer dude
Nov 23 '18 at 9:29





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.

– Some programmer dude
Nov 23 '18 at 9:29




3




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++.

– Yksisarvinen
Nov 23 '18 at 9:29





If you have troubles with understanding initialization list, you may want to have a look at our list of good books to learn C++.

– Yksisarvinen
Nov 23 '18 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.

– Blaze
Nov 23 '18 at 9:30







"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 9:30















Why would you want to do this?

– Robert Andrzejuk
Nov 23 '18 at 9:30







Why would you want to do this?

– Robert Andrzejuk
Nov 23 '18 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.

– luk32
Nov 23 '18 at 9:31







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 9:31














1 Answer
1






active

oldest

votes


















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






share|improve this answer


























  • 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.

    – gsamaras
    Nov 23 '18 at 12:38












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443846%2fdeconstruct-c-class-operator-overload-and-constructor-initialization-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer


























  • 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.

    – gsamaras
    Nov 23 '18 at 12:38
















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






share|improve this answer


























  • 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.

    – gsamaras
    Nov 23 '18 at 12:38














2












2








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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 9:37

























answered Nov 23 '18 at 9:32









gsamarasgsamaras

52.1k25107193




52.1k25107193













  • 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.

    – gsamaras
    Nov 23 '18 at 12:38



















  • 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.

    – gsamaras
    Nov 23 '18 at 12:38

















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







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.

– gsamaras
Nov 23 '18 at 12:38





@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.

– gsamaras
Nov 23 '18 at 12:38




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443846%2fdeconstruct-c-class-operator-overload-and-constructor-initialization-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini