简体   繁体   English

C ++应该重载函数还是语句足够?

[英]C++ should a function be overloaded or if statement is enough?

Good afternoon everyone. 大家下午好。

I have a question regarding function overloading in C++. 我对C ++中的函数重载有疑问。

There is a very big function in a class and my task is to make it more human readable and reduce it's complexity. 在一个类中有一个非常大的功能,我的任务是使它更易于阅读,并降低其复杂性。

In that function, almost two similar code blocks exist. 在该功能中,几乎存在两个类似的代码块。 The only difference is in some parameters and couple of additional lines. 唯一的区别在于某些参数和其他几行。

I want to create a function out of those blocks to avoid repeating, but how is it better to do? 我想从这些块中创建一个函数以避免重复,但是最好怎么做呢? Should I overload that new function or add an if statement inside function so that if one parameter has this or that value, then those couple of additional lines should be executed as well? 我应该重载该新函数还是在函数内添加if语句,以便如果一个参数具有该值或那个值,那么还应该执行这几对附加行?

What is the best approach? 最好的方法是什么?

The function looks like this 该功能看起来像这样

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

  //some code
}

So, I want to make a function out of the code blocks, but they are not completely the same and require some different operations performing right in the middle. 因此,我想用代码块来实现一个功能,但是它们并不完全相同,并且需要在中间执行一些不同的操作。 Example: 例:

void boo(){
  //code block
  //if(blah) execute this additional line
  //else this 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
}

So, as I understand it, your situation is like this: 因此,据我了解,您的情况如下:

int FirstFunction(int parameter_1, bool parameter_2) {
  // first large section of code shared by both
  // line unique to FirstFunction
  // second large section of code shared by both
}

int SecondFunction(long parameter_1, Dawg parameter_2) {
  // line unique to SecondFunction
  // first large section of code shared by both
  // second large section of code shared by both
}

Is this accurate? 这个准确吗? If so, I would just take out each section of code that is identical (or very similar) between the two functions, put that section into its own function, and call it from both. 如果是这样,我只需要取出两个函数之间相同(或非常相似)的代码的每个部分,将该部分放入自己的函数中,然后从两个函数中调用它。 Repeat until you have no duplicate code left: 重复直到没有重复的代码为止:

void FirstSharedCode(/* parameters */) {
  // first large section of code shared by both
}

void SecondSharedCode(/* parameters */) {
  // second large section of code shared by both
}

int FirstFunction(int parameter_1, bool parameter_2) {
  FirstSharedCode(/* parameters */);
  // line unique to FirstFunction
  SecondSharedCode(/* parameters */);
}

int SecondFunction(long parameter_1, Dawg parameter_2) {
  // line unique to SecondFunction
  FirstSharedCode(/* parameters */);
  SecondSharedCode(/* parameters */);
}

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, you could pass in a lambda function for the unique line: 或者,您可以为唯一行传递一个lambda函数:

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

and just call as such: 然后这样调用:

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

From the question statement the initial complex function kind of looks like this: 从问题陈述中看,初始复杂函数类型如下:

void f1(){
  //some code 
  {
     { <common_code_block_0> }
     <unique_code>
     { <common_code_block_1> }
  }
  {
     { <common_code_block_0> }
     <unique_code>
     { <common_code_block_1> }
  }

  //some code
}

There are two common code blocks which are repeated. 有两个重复的通用代码块。 These need to be isolated to reduce complexity and the likelihood of future bugs. 需要将这些隔离,以降低复杂性并减少将来出现错误的可能性。 Jut put each one of them in a separate function: Jut将它们中的每一个放在单独的函数中:

void function_0
{
   { <common_code_block_0> }
} 

void function_1
{
   { <common_code_block_1> }
} 

Taking it one step further declare these two functions as inline to avoid any overhead from the function call. 迈出一步,进一步将这两个函数声明为内联函数,以避免函数调用产生任何开销。

 inline  void function_0();    
 inline  void function_1();

Since there is some unique code executed between the two common_code blocks on each branch, probably the there are parameters to be passed in/returned to/from these new functions. 由于在每个分支的两个common_code块之间执行了一些唯一的代码,因此可能有一些参数要传入或传出这些新函数。 So they probably look something like: 因此,它们可能看起来像:

//parameters are I/O
inline void function_0(data_type* param1, data_type* param2, etc);
inline void function_1(data_type* param1, data_type* param2, etc);

And the initial f1() now looks like: 初始的f1()现在看起来像:

  void f1(){
  //some code 
  {
     function_0(&param1, &param2, etc);
     <unique_code>
     function_1(&param3, &param4, etc);
  }
  {
     function_0(&param1, &param2, etc);
     <unique_code>
     function_1(&param3, &param4, etc);
  }

  //some code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM