How do i make something like this? Is this even doable?
google "va_list"
It is. You want to #include <cstdarg> then declare your member function
as follows:
class Foo
{
// ...
public:
int my_variadic_function(const char *fmt, ...);
}
and in the function definition for Foo::my_variadic_function:
int Foo::my_variadic_function(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
// Use va_arg(ap, type) to get successive arguments, treating them
// as being of type 'type'.
// When you're done, clean up with va_end before returning; if you
// have multiple return points, *all* of them must call va_end.
va_end(ap);
return 0;
}
--
\_\/_/ some girls wander by mistake into the mess that scalpels make
\ / are you the teachers of my heart? we teach old hearts to break
\/ --- Leonard Cohen, "Teachers"
As has been stated in the thread, it is doable. Whether it's actually
simpler than using a list, I'm not sure...
- Gerry Quinn
Please note, however, that if you want to chain to another variadic
function, ie, vsprintf, you may need to use va_copy or risk crashes on
some platforms.
--
Jeff Lait
(POWDER: http://www.zincland.com/powder)
And if I recall correctly, the standard functions don't tell you when
the list ends, you have to figure that out yourself. Functions like
printf get that from the format string. Other alternatives are passing
the number of arguments before the actual list (might be a bit error-
prone) or having a list terminator element (don't forget it or this
thing will crash badly :) ). Another alternative is using the boost
preprocessor library, that has facilities to create multiple overloads
of the same function with different numbers of arguments, using some
preprocessor tricks. And finally, you can just overload the << or >>
operator to compose a list in a single line, like you do when you use
cout<<"hey"<<endl; (my favorite).
Jotaf