开发者

Avoiding function overloading

开发者 https://www.devze.com 2023-03-26 09:05 出处:网络
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

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.


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.


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.


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


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
}


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


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

精彩评论

暂无评论...
验证码 换一张
取 消