tags:

views:

74

answers:

4

I need to loop all the dynamic arguments i have gave to my function, how?

I mean this kind of code:

void something(int setting1, int setting2, ...){
   // loop somehow all the ... arguments and use setting1-2 values in that loop
}

setting1 and setting2 are not part of the dynamic argument list.

A: 

You could extract the variable arguments into a data structure (list, array, etc) and add setting1 / setting2 to that structure as well.

Then just loop over all of the structure's elements :)

Justin Ethier
how can i make dynamic structures...?
Newbie
You can make an array using brackets `[]`, but it is a fixed size you need to know how many elements are in the array (or a large enough max size).
Justin Ethier
yeah, and i dont want to count those with my fingers before using the arguments...
Newbie
+1  A: 

This MSDN article contains an explanation and a reasonable example.

Note: the whole va_arg business is not very safe, and as others suggested if all of those arguments are of the same type, just put them all in a container and pass that instead.

e.g:

void func(int arg1, int arg2, std::vector<float> &myFloats)
{
    // myFloats can be used here, and you know how many floats there are
    // by calling myFloats.size()
} 
Dmitry
but i want to write it like: func(0, 1, 4, 5, 1, 4) or func(0, 1, {4, 5, 1, 4}) etc. Not like: list.push_back(4);list.push_back(5);list.push_back(1);list.push_back(4); func(0, 1, list); :D
Newbie
@Newbie: are the arguments of the same type or not? And, if it is not a secret, what are you planning to do with them?
Dmitry
Im checking the dynamic 2d array sizes, if they are too small, i reject and quit the function from executing and push error. So far repeating this code seems to work fine, i just wish i could pack it up in one function that wont make it slower...
Newbie
`std::vector`s are very nice arrays, especially when it comes to checking sizes, which is _very_ fast. And, BTW, keep in mind that your compiler might be very good in inlining your functions, don't bother yourself unless absolutely necessary.
Dmitry
+2  A: 

Use va_ functions. Here's an example:

void PrintFloats ( int amount, ...)
{
    int i;
    double val;
    printf ("Floats passed: ");
    va_list vl;
    va_start(vl,amount);
    for (i=0;i<amount;i++)
    {
        val=va_arg(vl,double);
        printf ("\t%.2f",val);
    }
    va_end(vl);
    printf ("\n");
}

Your other arguments have to indicate how many other args there are. printf, for example, uses the percents in the format string to do this. My example uses the first arg.

And lest you think that this type of argument passing is the bees knees, be sure to read varargs are bad, m'kay.

plinth
OH crap... then i cant use this method. >_>
Newbie
Is it somehow possible to generate a template function that will repeat some part of the code? So i could generate codes for each amounts of arguments.
Newbie
+1  A: 

Like this:


#include <stdarg.h>

void Foo(int s1, int count, ...)
{
        va_list args;

        va_start(args, count);

        if (count > 0) {
            // Do something with the first variable, assuming it's std::string...
            std::string v1(va_arg(args, std::string));

            for (unsigned int i = 0; i < count - 1; ++i)
                    std::cout << va_arg(args, std::string) << "|";
        }

        va_end(args);
}
nhaa123