Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: Pixel_Outlaw on May 21, 2011
-
I recently needed to make a function with a variable amount of parameters.
To my surprise it turns out that you can indeed do such a thing in C++.
http://www.go4expert.com/forums/showthread.php?t=17592
How good/bad of an idea is this? Is this "feature" well known enough to include it in publishable code?
Essentially I'm making a vector font and I'd like to feed each Letter object an undefined number of Point objects in a single constructor.
-
Thats an interesting find, since it's using the stdarg part of the standard library then I would say its a feature you can use, since the standard library is considered part of the languages ISO standard. Tho I must admit I never encoutered this one before, but I have never really delved into all the standard lib stuff beyond what I needed.
-
Not a problem if you use it right. This same feature even made it into C#.
Jim
-
The stdarg stuff is ok. Maybe a bit too much C, and not C++ design but well, if you don't mind it's ok. Just for your knowledge, printf / scanf (and all derivated) are using stdargs.
Maybe you can replace it, with a std::vector, or a template ... but it depends of your code :D
-
Please correct my understanding if I'm wrong...
What the author does not mention is that you can only pass Plain Old Data into functions which utalize "..." or the ellipsis.
I tried to make a constructor for a wire frame "Letter" object that would take an unlimited number of Point objects and the compiler refused to build it.
Letter(Point pt, ...)
-
Hum ... I am still really wondering why not using a list of Points ... looks like it's the same ...
-
I probably would have done that if you could initialize a known series of elements in a list in a single line.
...or can one?
See the problem is calling around 8 "push backs". It gets very ugly when you have to construct 40 letters for a sroller.
There is a solution here where the author parses string data with "," delimters between the string letters.
I just threw my hands in the air and went with fixed size arrays (since arrays can be initialized with data in a single line)
http://www.bdsoft.com/tools/initutil.html
-
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:
myfunc(ARG1, ARG2, ARG3, .... ARGn, NULL);
And inside myfunc:
#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.
-
myfunc << arg1 << arg2 << arg3;
And C++11 offers initializer lists (http://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists).