By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

passing varg to another method

P: n/a
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");
}

Apr 21 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* coltrane:
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, ....)
A C++ ellipsis is three periods, not four.

{
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 *){
'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.

vfoo("this is a %s", "test");
}


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?
Apr 21 '06 #2

P: n/a
>>A C++ ellipsis is three periods, not four.
int main()

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?

Apr 21 '06 #3

P: n/a
* coltrane:
A C++ ellipsis is three periods, not four.
int main()

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?


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?
Apr 21 '06 #4

P: n/a
coltrane wrote:
Is it possible to have a function with variable number of parameters
pass the parameters to another method that takes variable number of
params?
No, not really.
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);
}


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

Apr 21 '06 #5

P: n/a
>>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

Apr 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.