
April 21st, 2006, 03:35 PM
| | | passing varg to another method
Is it possible to have a function with variable number of parameters
pass the parameters to another method that takes variable number of
params?
maybe something like this
void vfoo2( const char * format, ....)
{
va_list parg;
va_start(parg, format);
vsprintf(buffer, format, parg);
va_end(parg);
}
void vfoo(const char* format, ....)
{
va_list parg;
va_start(parg, format);
vfoo2(format, parg)
va_end(parg);
}
main(int , const char *){
vfoo("this is a %s", "test");
} |

April 21st, 2006, 03:45 PM
| | | Re: passing varg to another method
* coltrane:[color=blue]
> Is it possible to have a function with variable number of parameters
> pass the parameters to another method that takes variable number of
> params?
>
> maybe something like this
>
> void vfoo2( const char * format, ....)[/color]
A C++ ellipsis is three periods, not four.
[color=blue]
> {
> va_list parg;
> va_start(parg, format);
> vsprintf(buffer, format, parg);
> va_end(parg);
> }
>
> void vfoo(const char* format, ....)
> {
> va_list parg;
> va_start(parg, format);
> vfoo2(format, parg)
> va_end(parg);
> }
>
>
> main(int , const char *){[/color]
'main' must have result type 'int'.
For the rest, the standard allows an implementation to support any
signature whatsoever.
However, most likely your compiler does not support the non-standard
signature you've chosen, and there's absolutely no point in listing the
formal arguments of 'main' when you don't name them; use
int main()
instead.
[color=blue]
> vfoo("this is a %s", "test");
> }[/color]
Yes, it's possible. But not in standard C++. In standard C++ you have
much better, type safe ways to achieve just about anything you'd use
that for, so, describe what you want to achieve (not how).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? | 
April 21st, 2006, 03:55 PM
| | | Re: passing varg to another method
>>A C++ ellipsis is three periods, not four.[color=blue][color=green]
>>int main()[/color][/color]
I'm not planning on compiling the message
the first method, vfoo will generate a new format string.
and then pass it to the second method, vfoo2.
The list of args are same.
A: rude replies
Q: What is the most annoying thing on usenet and in e-mail? | 
April 21st, 2006, 04:05 PM
| | | Re: passing varg to another method
* coltrane:[color=blue][color=green][color=darkred]
>>> A C++ ellipsis is three periods, not four.
>>> int main()[/color][/color]
> I'm not planning on compiling the message
>
> the first method, vfoo will generate a new format string.
> and then pass it to the second method, vfoo2.
> The list of args are same.
>
> A: rude replies
> Q: What is the most annoying thing on usenet and in e-mail?[/color]
Was my answer not useful?
I thought it would be.
What did you find rude?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? | 
April 21st, 2006, 05:15 PM
| | | Re: passing varg to another method
coltrane wrote:[color=blue]
> Is it possible to have a function with variable number of parameters
> pass the parameters to another method that takes variable number of
> params?[/color]
No, not really.
[color=blue]
> maybe something like this
>
> void vfoo2( const char * format, ....)
> {
> va_list parg;
> va_start(parg, format);
> vsprintf(buffer, format, parg);
> va_end(parg);
> }
>
> void vfoo(const char* format, ....)
> {
> va_list parg;
> va_start(parg, format);
> vfoo2(format, parg)
> va_end(parg);
> }[/color]
Well, why do you think you need vsprintf? If the mechanism worked as
you'd
expect it to, you could just pass parg to sprintf. Of course, you could
define
vfoo2 to take one valist instead of a variable number of params, just
like
vsprintf. But do you really want to do that? vararg functions are
tricky.
HTH,
Michiel Salters | 
April 21st, 2006, 07:25 PM
| | | Re: passing varg to another method
>>Of course, you could define vfoo2 to take one valist instead of a variable number of params, just like vsprintf.
so simple it didn't occur to me. I was locked into the idea of having
to use '...' and not va_list as a param.
It would be a little long explaining why I want to do such a messy
thing but it helps.
thanks
john | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,414 network members.
|