避免函数重载

发布于 2024-11-28 01:11:40 字数 795 浏览 2 评论 0原文

在下面的程序中,我有一个函数重载。一个只有一个参数,另一个有两个参数,另一个有三个参数。在下面的示例中,它看起来很简单,因为函数不太长。如果函数很长并且使用不同的输入参数一次又一次地编写相同的函数看起来很丑怎么办?一种方法是使用可变参数函数。如果我知道我的函数只需要 1,2 或 3 个输入参数,那么可变参数函数真的有必要吗?如果是这样我该怎么做?注意:具有三个输入参数和两个输入参数的函数执行不同的计算。

#include <iostream>
using namespace std;

int function(int a, int b, int c)  // All the arguments are always of the same type
{
    return a*b*c;
}

int function(int a, int b)
{
    int c = a; // Always duplicate the first argument
    return a*b*c;  
}
int function(int a)
{
    int b = a, c = a; // Always duplicate the first argument
    return a*b*c;
}
int main()
{
    cout<<function(2,3,4)<<"\n"<<function(2,3)<<"\n"<<function(2);
    cin.ignore();
    return 0;
}

编辑:

很抱歉各位的含糊之处。我编辑了代码。

In the following program I've a function overloading. One with just a single argument, another with two arguments and another with three. In the following example it looks simple because the function is not too long. What if the function is very long and it looks ugly to write the same function again and again with different input arguments. One way to do that can be variadic functions. If I know that my function is going to take only 1,2 or 3 input arguments is variadic functions really necessary ? If so how can I do that ? Note : the function with three input args and two input args perform different calculations.

#include <iostream>
using namespace std;

int function(int a, int b, int c)  // All the arguments are always of the same type
{
    return a*b*c;
}

int function(int a, int b)
{
    int c = a; // Always duplicate the first argument
    return a*b*c;  
}
int function(int a)
{
    int b = a, c = a; // Always duplicate the first argument
    return a*b*c;
}
int main()
{
    cout<<function(2,3,4)<<"\n"<<function(2,3)<<"\n"<<function(2);
    cin.ignore();
    return 0;
}

EDIT:

Sorry for the ambiguity guys. I edited the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

穿越时光隧道 2024-12-05 01:11:41

看来你应该使用数组来代替:

void Function(int* items, int count)
{
    int result = 1;
    for(int i = 0; i < count; i++)
    {
        result *= items[i];
    }
}

It looks like you should be using an array instead:

void Function(int* items, int count)
{
    int result = 1;
    for(int i = 0; i < count; i++)
    {
        result *= items[i];
    }
}
美人骨 2024-12-05 01:11:40

首先,如果您的函数又长又丑,您应该将其重构为一组较小的函数甚至类。

至于您的实际问题,我将使用重载函数作为包装器,如下所示:

int function(int a, int b, int c)
{
  return a * b * c;
}

int function(int a, int b)
{
  return function(a, b, a);
}

int function(int a)
{
  return function(a, a, a);
}

这可以避免代码重复和对可变参数函数的任何需要。使用可变参数函数,您会失去静态类型检查,因此它们很容易出错。

First of all if your function is long and ugly you should refactor it into a set of smaller functions or even classes.

As to your actual question, I would use the overloaded functions as wrappers like this:

int function(int a, int b, int c)
{
  return a * b * c;
}

int function(int a, int b)
{
  return function(a, b, a);
}

int function(int a)
{
  return function(a, a, a);
}

This avoids code duplication and any need for a variadic function. With variadic functions you lose the static type checking, so they are very error prone.

逆流 2024-12-05 01:11:40

任何使用可变参数函数的解决方案都会更糟糕:首先,可变参数函数不知道有多少个参数,也不知道它被调用的类型,您需要额外的参数才能知道这一点。

Any solution with variadic functions will be worse: for a start, a variadic function doesn't know with how many argument nor of which type it was called, you need additional arguments to know that.

难如初 2024-12-05 01:11:40

我认为解决方案是只有一个带有签名函数(int,int,int)的函数。

如果您想复制其他变体的行为,可以使用 function(a,b,a) 而不是 function(a,b) 显式地执行此操作。

I think the solution would be to just have one function with a signature function(int,int,int).

If you want to copy the behavoir of the other variations, you can do it explicitly, with function(a,b,a) instead of function(a,b).

遗弃M 2024-12-05 01:11:40

在您的问题中,您指出多次编写相同的长函数很烦人,而且三输入和两输入版本不同。是哪一种情况呢?

  • 如果他们正在做同样的事情,只需调用其中一个即可。通常情况下,参数较少的函数会在链上多一个参数调用它们的直接上级,或者所有重载都调用单个最多参数的版本。

  • 如果它们正在做不同的事情,那么您可能一开始就没有理由让它们超载。给它们起不同的名字可能会让事情变得更清楚,并减轻“两次编写同一个函数”的感觉。

  • 如果他们正在做类似的事情,即介于上述两种情况之间,您可能需要额外的函数来分解原始重载函数的相同部分。

您的示例属于第一类:

int function(int a, int b, int c)  // All the arguments are always of the same type
{
    return a*b*c;
}

int function(int a, int b) // Always duplicate the first argument
{
    return function(a,b,a);  
}

int function(int a) // Always duplicate the first argument
{
    return function(a,a);
//  return function(a,a,a); //might make more sense depending on actual function/variable names
}

In your question you state both that writing the same, long function multiple times is tiresome AND that the three- and two-input versions differ. Which is the case?

  • If they're doing the same thing, simply call one from the other. It usually happens to be the case that functions with lesser parameters call their immediate superior with one more parameter, up the chain, or all the overloads call the single most-parameter version.

  • If they're doing different things, you probably have no reason to overload them in the first place. Calling them different names may clear things up and ease the feeling of "writing the same function twice."

  • If they're doing similar things, i.e. fall in somewhere between the two cases above, you probably need extra functions to factor out the identical portions of the original, overloaded functions.

Your example falls into the first category:

int function(int a, int b, int c)  // All the arguments are always of the same type
{
    return a*b*c;
}

int function(int a, int b) // Always duplicate the first argument
{
    return function(a,b,a);  
}

int function(int a) // Always duplicate the first argument
{
    return function(a,a);
//  return function(a,a,a); //might make more sense depending on actual function/variable names
}
痴意少年 2024-12-05 01:11:40

如果计算不同是不同的函数,那么您没有任何理由避免重载。

Evaen,如果 calc 相同,你应该避免 Variadic_function ,它可能会提供很多错误(类型、参数计数等)

此外,尝试将这 3 个函数作为 1 个变量执行,并说它不那么难看。我会笑

另外,参数计数的切换是运行时(如在varidiac_function中)比编译时更糟糕

If calculations is different is different functions you have not any reasons to avoid overloading.

Evaen if calc are same you should avoid of Variadic_function that may provide a lot of errors(with types, arguments count, etc)

Besides, try to do this 3 funcs as 1 variadic and say that it's less ugly. I will laugh

Also switching for arguments count is runtime( as in varidiac_function) is worst than in compile-time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文