Sign in to follow this  

Possible to overload with same function body?

Recommended Posts

Hi Guys,

 

Is it possible to overload a function while keeping the same function body?

 

Something like this?

 

int some_function(int input1)
int some_function(int input1, int input2)
{
     for(int I=0; I<arg_count_from_function; i++)
     {
             // do something here with iterating through the args
     }
     return 0;
}

 

I have a scenario where there is some code/function repetition but the only real difference between them is the argument count being sent to the functions.

 

Thanks in advance :)

Share this post


Link to post
Share on other sites

If the parameter count is more variable then just 2, this might be very interesting for you : http://www.cplusplus.com/reference/cstdarg/va_list/

void f (int p, ...)
{
  va_list v;
  va_start(v,p);

  for (i=0; i < p; i++)
  {
    int value = va_arg(v, int);
    //use value
  }

  va_end(v);
}
Edited by NLScotty

Share this post


Link to post
Share on other sites

Just pass your arguments as a reference to a vector<string> or vector<int> or whatever you're passing in. One function. If you need to know how to convert strings to ints, use an istringstream. Let me know if you run into trouble.

 

Use the command g++ filename.cpp -std=c++14 -stdlib=libc++ on Mac OS X.

#include <vector>
#include <string>
#include <iostream>
using namespace std;
 
void parse_vars(const vector<string> &vars)
{
    for(const string var : vars)
        cout << var << endl;
}
 
int main(int argc, char **argv)
{
    vector<string> vars;
    
    for(int i = 0; i < argc; i++)
        vars.push_back(argv[i]);
 
    parse_vars(vars);
 
    return 0;
}
Edited by sjhalayka

Share this post


Link to post
Share on other sites
EDIT:
Please remember to stay on-topic— I’ve cleaned up some of the off-topics posts here.





Is it possible to overload a function while keeping the same function body? It depends.

If an argument is optional, then supply a default. See TheComet’s and Nypyren’s suggestions. This is not appropriate for functions that work over a range of data because you could only work with as many elements as there are parameters. For example, what good is an averaging function, if it could only average up to 5 elements?

If the function needs to perform some work over a range of data (`avg`, for example), then the function should accept an array or range. See Sjhalayka’s suggestion. Variadic arguments could work, but it is not type safe, and it cannot be used with runtime data.


By the looks of the code you posted, you should be using an array or range instead. Edited by fastcall22

Share this post


Link to post
Share on other sites

 

If the parameter count is more variable then just 2, this might be very interesting for you : http://www.cplusplus.com/reference/cstdarg/va_list/

void f (int p, ...)
{
  va_list v;
  va_start(v,p);

  for (i=0; i < p; i++)
  {
    int value = va_arg(v, int);
    //use value
  }

  va_end(v);
}

Don't do this; it's not type safe. There is a type-safe alternative using variadic templates. However, you may still be better off with a function that takes a container or range.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this