Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++]Variable argument functions (...)

Started by d000hg May 28, 2006 at 2:41 AM 5 replies 1.2k views
Original Post
d000hg
d000hg
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.
joanusdmentia
joanusdmentia
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
CTar
CTar
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 size	vsprintf( 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 times	MyPrintf_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.
etothex
etothex
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 :)
Enigma
Enigma
Quote:
Original post by SiCrane, forever imortalized in Zahlman's sig
Basically whenever you invoke the dread ellipses construct you leave the happy world of type safety.

Σnigma
MaulingMonkey
MaulingMonkey
Quote:
Original post by Enigma
Quote:
Original post by SiCrane, forever imortalized in Zahlman's sig
Basically whenever you invoke the dread ellipses construct you leave the happy world of type safety.

Σnigma


QFE. 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.
d000hg
d000hg
Quote:
Original post by etothex
If 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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.