   [C++]Variable argument functions (...)
  If I have two functions:void MyPrintf(char *str,...);void LogMessage(char *str,...);Is it possible for LogMessage to call MyPrintf, passing all its own parameters across? If so, a code snippet would be great.Thanks.   
  AFAIK the answer's no, at least not directly.  Instead, have a common function that accepts the argument list (type va_list) that both functions call, look up the vprintf functions to see what I mean.   ;  If you are not entirely sure what joanusdmentia meant, then something like this:#include <iostream>#include <cstdarg>#include <cstdio>void MyPrintf_impl(char* str,va_list arg_list){char buf[512]; // In general it's a bad idea to depend on a const sizevsprintf( buf,str,arg_list );std::cout << buf << std::endl;}void MyPrintf(char* str,...){va_list arg_list;va_start( arg_list, str );MyPrintf_impl( str, arg_list);va_end( arg_list );}void LogMessage(char *str,...){va_list arg_list;va_start( arg_list, str );// Print three timesMyPrintf_impl( str, arg_list );MyPrintf_impl( str, arg_list );MyPrintf_impl( str, arg_list );va_end( arg_list );}Of course if you haven't created MyPrintf yourself, you can't do this.   ;  If you don't have access to change MyPrintf, then you can just chain it through vsnprintf as below:#include <iostream>#include <cstdarg>#include <cstdio>void MyPrintf(char* str,...);void LogMessage(char *str,...){va_list arg_list;va_start( arg_list, str );   char buf[512];   vsnprintf(buf, 512, str, arg_list)MyPrintf(buf);va_end( arg_list );}but you lose some flexibility here. All this is one reason why varargs suck in general :)   ;Quote:Original post by SiCrane, forever imortalized in Zahlman's sigBasically whenever you invoke the dread ellipses construct you leave the happy world of type safety.Σnigma   ;Quote:Original post by EnigmaQuote:Original post by SiCrane, forever imortalized in Zahlman's sigBasically whenever you invoke the dread ellipses construct you leave the happy world of type safety.ΣnigmaQFE.  I'd prefer operator chaining - if you want to use format strings, take a look at Boost.Format which in conjunction with operator chaining achieves similar effect to printf() & co., in a type safe, object safe manner that does not invoke the dread ellipses.   ;Quote:Original post by etothexIf you don't have access to change MyPrintf, then you can just chain it through vsnprintf as below:*** Source Snippet Removed ***but you lose some flexibility here. All this is one reason why varargs suck in general :)I think I'm going to have to approach it this way.Thanks - all very interesting.