Dynamic function arguments in C++, possible?
Posted
by
Jeshwanth Kumar N K
on Stack Overflow
See other posts from Stack Overflow
or by Jeshwanth Kumar N K
Published on 2012-10-25T04:29:51Z
Indexed on
2012/10/25
5:00 UTC
Read the original article
Hit count: 141
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 fuctions 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;
}
© Stack Overflow or respective owner