3

I am little new to C++, I have one doubt in variable argument passing. As I mentioned in a sample code below ( This code won't work at all, just for others understanding of my question I framed it like this), I have two functions func with 1 parameter and 2 parameters(parameter overloading). I am calling the func from main, before that I am checking whether I needs to call 2 parameter or 1 parameter. Here is the problem, as I know I can call two functions in respective if elseif statements, but I am curious to know whether I can manage with only one function. (In below code I am passing string not int, as I mentioned before this is just for others understanding purpose.

#include<iostream.h> #include <string> void func(int, int); void func(int); void main() { int a, b,in; cout << "Enter the 2 for 2 arg, 1 for 1 arg\n"; cin << in; if ( in == 2) { string pass = "a,b"; } elseif ( in == 1) { string pass = "a"; } else { return 0; } func(pass); cout<<"In main\n"<<endl; } void func(int iNum1) { cout<<"In func1 "<<iNum1<<endl; } void func(int iNum1, int iNum2) { cout<<"In func2 "<<iNum1<<" "<<iNum2<<endl; } 
3
  • You can also make the 2 parameter func's iNum2 have a default value by doing void func(int iNum1, int iNum2 = 0). Commented Oct 25, 2012 at 4:35
  • @Asha: I don't think default value would serve his purpose. It always calls void func(int,int) but never func(int). Commented Oct 25, 2012 at 4:51
  • 1
    Compiler needs to know number of function arguments at compile time to pick a proper function overload. So the answer to your question is generally "No", there is no such thing as "dynamic arguments" in C++. However, you could pass vector of parameters or any other "dynamic" structure, as @ozox suggested. Commented Oct 25, 2012 at 5:02

2 Answers 2

4

You may use stdarg method as explained by Ronald. Or you could use a function that takes a vector of arguments. Push the arguments to the vector first and then call following func:

func(const vector<int>& argv) { for (vector<int>::const_iterator iter = argv.begin(); iter != argv.end(); ++iter) { // Get the arguments } // Do what you want ... } 
Sign up to request clarification or add additional context in comments.

Comments

3

I believe you may be interested in looking at the stdarg library. A sample usage is as follows,

#include <cstdarg> #include <iostream> using namespace::std; void func(int, ...); int main(void) { func(1, 10); func(2, 20, 30); return 0; } void func(int num_args, ...) { va_list ap; va_start(ap,num_args); for(size_t loop=0;loop<num_args;++loop) { if(loop>0) cout << " "; cout << va_arg(ap,int); } va_end(ap); cout << endl; } 

2 Comments

Hi @Ronald, thanks for your kind reply, but I am looking for other way round, two different functions (operator overloading), and here number of arguments in function call is variable.
Hi @JeshwanthKumarNK, I might be missing something here and if you can shed some more light on the intended use case, we may try something else further. (Or maybe ozox has got the solution?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.