1
C / C++ /C# / Re: [C++] Functions with undefined number of parameters.
« on: February 13, 2012 »
Maybe you've already made up your mind, but just one thing worth mentioning:
Although you can use this like how printf() does, you can have it be NULL delimited too.
As in all on one line:
And inside myfunc:
In C++ there is probably someway to make a linked list class and overload operators so you can use << or + or something, so you could have a line like:
myfunc << arg1 << arg2 << arg3;
or myfunc + arg1 + arg2 + arg3;
But I don't know enough about C++ to know if that is true or what the pitfalls might be.
Although you can use this like how printf() does, you can have it be NULL delimited too.
As in all on one line:
Code: [Select]
myfunc(ARG1, ARG2, ARG3, .... ARGn, NULL);And inside myfunc:
Code: [Select]
#include <stdio.h>
#include <stdarg.h>
void myFunc(void *FirstObject, ...)
{
void* object=FirstObject;
va_list ap;
va_start(ap, FirstObject);
/*--get it started--*/
do
{
/*--do your thing with object--*/
printf("%s ",object);
object = va_arg(ap,void*);
} while(object!=NULL);
va_end(ap);
}
void myFunc2(unsigned firstNum, ...)
{
unsigned num=firstNum;
va_list ap;
va_start(ap, firstNum);
/*--get it started--*/
do
{
/*--do your thing with object--*/
printf("%d ",num);
num = va_arg(ap,unsigned);
} while(num!=0xDEADBEEF);
va_end(ap);
}
int main(int argc, char *argv[])
{
myFunc("Hello, world!\n","what's up?","yoyoyo",NULL);
myFunc2(1,2,3,4,5,6,7,8,0xDEADBEEF);
return 0;
}
In C++ there is probably someway to make a linked list class and overload operators so you can use << or + or something, so you could have a line like:
myfunc << arg1 << arg2 << arg3;
or myfunc + arg1 + arg2 + arg3;
But I don't know enough about C++ to know if that is true or what the pitfalls might be.