I'm trying to do an object implementation of va_list
. It's complex to explain with word, so look at the code :
class auto_va
{
public:
va(int n)
{
va_start(varg, n);
}
~va()
{
va_end(varg);
}
private:
va_list varg;
public:
va_list& get_va()
{
return varg;
}
};
double sum(int number, ...)
{
int result(0);
auto_va varg(number);
for (;number != 0; --number)
result += va_arg(varg.get_va(), int);
return result;
}
int main()
{
std::cout << sum(6, 1, 5, 3, 4, 7, 8) << std::endl;
}
The goal is an automatic va_end
when the end of the block is reached. The problem is that I can't use va_start
in a fixed parameter function, so, the code above don't compile.
I know I'm mixing old-school outdated C and C++ object features, but the problem still strange. Is there an interesting explanation or something ?
Finally, is there a correct alternative to what I'm trying to do, or an alternative to C-style infinite parameters function?