If you have a function which can accept a variable number of arguments, how can you find out how many arguments were passed to it?
This is a discussion on Newbie question about Variable Argument Lists for Functions within the C++ Programming forums, part of the General Programming Boards category; If you have a function which can accept a variable number of arguments, how can you find out how many ...
If you have a function which can accept a variable number of arguments, how can you find out how many arguments were passed to it?
Either manually (passing number of arguments as an argument), or by fetching argument by argument until you come across a "known value" that is used to mark the end of arguments (ie NULL), or if the arguments are all of the same type, you can use a vector and use its .size() method.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Incidentally, are you sure that you need variable argument lists in the first place?
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Awesome, thanks. And thanks for replying with the "safe delete" function in my other thread, too!
Unless the function expects different types, in order. But generally, you are right, of course.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
yes, that is true if there is an assumed repeating pattern to the types passed. however it may well still be preferable, even if arguably marginally less convenient, to pass a vector of structs.
just yesterday i made a polynomial function that uses a variable argument constructor, where the constructor arguments go: unsigned int order, int exp1,double coeff1,...int expn,double coeffn
and what did i end up having to do immediately thereafter?
declaringwithin the body of polynomial to store the data in a vector. in then end it would have been vetter to just pass a vector<polynomial::ecpair>Code:struct ecpair{int exp;double coeff;}
the only thing i can really see variable argument functions providing unique functionality is is where the first argument is a typelist, as then there need be no pattern, but then my compiler doesn't match the function so it doesn't work anyway. :\