Skip to main content
gamedev.net
🔒 Locked

c++ funcs - undefined parameter number

Started by 41ph4_r4p70r Oct 11, 2004 at 10:34 AM 11 replies 800+ views
Original Post
41ph4_r4p70r
41ph4_r4p70r
Hey guys, anyone could help me out, I need to create a function with an undefined number or params (like printf() for instance), I need to know how to define one and how to know inside the body, how many params where passed by the user. Any help appreciated
Magius
Magius
Look into the va_list, va_start, va_arg, and va_end macros - reading the documentation on those should provide you with some examples on how to do it.

A nice link is here.

Magius
Enigma
Enigma
If you're using C++ there is almost certainly a better way to do it which does not circumvent the type system. What is it that you are trying to do?

Enigma
41ph4_r4p70r
41ph4_r4p70r
Basically, I need a function that draws a poly (obviously, I have no idea of how many sides does the poly has), then I need a func that get as its input an undetermined number of points, that then I ll use to draw the poly as I said before.

Btw, Im under a linux box
Ra
Ra
You could pass a pointer to an array of points as one argument and the number of points as another.
Ra
Paradigm Shifter
Paradigm Shifter
Or you could make a Polygon class that has a std::vector of Points and knows how to draw itself. I know which one I'd prefer.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
41ph4_r4p70r
41ph4_r4p70r
Certainly, passing a pointer would be a suitable choice, but while Im here, I wanna learn how to make it the way I explained ... :)

And BTW again, I need to pass an extra parameter, something like

void func((int)+,QColor) or void func(QColor,(int)+)

and I think va_* are macros defined only for Microsoft compiler, and again, Im under a Linux box
Chozo
Chozo
The va_* macros are standard C (in stdarg.h). If you can get ahold of K&R's C book, they give a good explanation of how to use them.
41ph4_r4p70r
41ph4_r4p70r
Yep, with that last #include I got it working, thx everyone for the help
Paradigm Shifter
Paradigm Shifter
Don't use them though.
Your original strategy was flawed, output until you get a zero float (actually a double because of the weird ellipsis promotion rules), what if your polygon had a point at the origin.
The argument count in the parameter list is better, but it's still prone to mistakes (miscounting number passed).

printf etc. is flawed because you can pass less types than are extracted resulting in bogus output, or the wrong format specifier (bad output again).

You are coding in C++. Use it. Don't use features from C that have a better way to do the same thing in C++. Would you use #define instead of an inline function?
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
41ph4_r4p70r
41ph4_r4p70r
Yeah, after my last post, I came to realize that wasnt the best choice after all since theres no fool proof in finding out how many parameters were passed , and yes, asking the user to put a last centinell number is stupid.

Btw, If i define an argument argc and argv (as in main()) into any function, Can I use em as I do with main?
for(int i=0;i cout<<
or some like that?
Paradigm Shifter
Paradigm Shifter
Nope, the console launcher takes the command line parameters and builds the parameters automatically when the executable is loaded into memory.
You'd have to build similar parameters yourself in that case. (i.e. a number of arguments and an array of pointers to char*). That's also a legacy left over from C.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.