Jan 18, 2010 at 9:54am UTC
hello,
can anybody tell me if stdarg.h supports variable list of arguments having different types? I saw a lot of examples where a function takes variable number of integers and finds the max of it but can we have functions accepting different types of variable number of arguments. If yes, can someone give a small example?
Thanks
Suraj
Jan 18, 2010 at 11:37am UTC
Last edited on Jan 18, 2010 at 11:41am UTC
Jan 18, 2010 at 11:55am UTC
Hi...
Is this what you are expecting????????...
// Function template example.
#include <iostream>
using namespace std;
// This is a function template.
template <class X> void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int i=10, j=20;
double x=10.1, y=23.3;
char a='x', b='z';
cout << "Original i, j: " << i << ' ' << j << '\n';
cout << "Original x, y: " << x << ' ' << y << '\n';
cout << "Original a, b: " << a << ' ' << b << '\n';
swapargs(i, j); // swap integers
swapargs(x, y); // swap floats
swapargs(a, b); // swap chars
cout << "Swapped i, j: " << i << ' ' << j << '\n';
cout << "Swapped x, y: " << x << ' ' << y << '\n';
cout << "Swapped a, b: " << a << ' ' << b << '\n';
return 0;
}
Jan 18, 2010 at 8:29pm UTC
Whatever it is you are doing, there is surely a better way to do it.
What are you trying to accomplish with this?