🔒 Locked
c++ funcs - undefined parameter number
Advertisement
Original Post
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
You're looking for something called ellipsis. Here's a good tutorial http://www.codeproject.com/cpp/argfunctions.asp.
Good luck
Good luck
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
Btw, Im under a linux box
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.
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
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
Yep, with that last #include I got it working, thx everyone for the help
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?
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?
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?
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
or some like that?
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement