C++ should a function be overloaded or if statement is enough? -


good afternoon everyone.

i have question regarding function overloading in c++.

there big function in class , task make more human readable , reduce it's complexity.

in function, 2 similar code blocks exist. difference in parameters , couple of additional lines.

i want create function out of blocks avoid repeating, how better do? should overload new function or add if statement inside function if 1 parameter has or value, couple of additional lines should executed well?

what best approach?

the function looks this

void f1(){   //some code    {   //some code block   //some other line belongs block   //the code block continues   }   {   //the same kind of code block   //this line different   //the code block continues   }    //some code } 

so, want make function out of code blocks, not same , require different operations performing right in middle. example:

void boo(){   //code block   //if(blah) execute additional line   //else line   //code block } 

or overloading

void boo(param1, param2){    //code block    //unique line    //code block }  void boo(param1, param2, param3){    //code block    //unique line    //code block } 

you can change

void boo(param1, param2){     //code block     //unique line     //code block } void boo(param1, param2, param3){     //code block     //unique line     //code block } 

into

void baa() {     // code block1 }  void bee() {     // code block2 }  void boo(param1, param2){     baa();     //unique line     bee(); } void boo(param1, param2, param3){     baa();     //unique line     bee(); } 

or alternatively, pass in lambda function unique line:

void boo(std::function<int()> unique) {     // code block     int bleh = unique();     // code block } 

and call such:

boo([]() { return 1; }); boo([]() { return 3; }); 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -