How to write such a function?

T

tings

How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?

Thanks for for your help!
 
A

Artie Gold

tings said:
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?

Thanks for for your help!
See, for example:

http://www.gnu.org/software/libc/manual/html_node/Variadic-Example.html#Variadic Example

Of course, you *could* have searched the web for `variadic function
C++'...but I'll give you this one as a freebie. ;-)

[Of course, since this is the include you should use
is <cstdarg> as opposed to <stdarg.h>.]

HTH,
--ag
 
D

David Harmon

On Sun, 09 Jan 2005 19:15:07 GMT in comp.lang.c++, "tings"
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?

/* VA_EXAMP.C - variable argument function example, subset of printf() */
/* Released to public domain by author, David Harmon, Oct 1993 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

void va_example(char *format, ...)
{
va_list ap;
char ch;

va_start(ap, format);

/* Note: because of default promotions, you can't use char,
or float types with va_arg. Use int or double instead. */

while ((ch = *format++) != 0) {
if (ch != '%')
fputc(ch, stdout);
else {
if ((ch = *format++) == 0)
break;
switch (ch) {
case 'd': {
int arg = va_arg(ap, int);
char buf[10];
itoa( arg, buf, 10);
fputs( buf, stdout);
break;
}

case 'c': {
int arg = va_arg(ap, int);
fputc( (char)arg, stdout);
break;
}

case 's': {
char *arg = va_arg(ap, char *);
fputs(arg, stdout);
break;
}

default:
fputc('%', stdout);
fputc(ch, stdout);
}
}
}
va_end(ap);
}


int main(void)
{
va_example("\"%s\" is a string, %c is a char, and %d is an integer.\n",
"Who is John Galt?", '$', -1);
return 0;
}
 
A

Alf P. Steinbach

* tings:
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?

Don't. Use the type-safe idiom exemplified by std::cout. I.e., member
functions or operators that return a reference to the object they're
called on, so that you can tack on further calls.
 
S

Siemel Naran

How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?

In standard C++, the preferred way to do this would be to:

(1) Create an abstract base class Variable with virtual functions, derived
class Int and so on from it, create a std::vector<Variable*> though
std::vector<boost::shared_ptr<Variable> > might be better in terms of memory
management.

(2) Create a std::vector<boost::any>.

If all your types are fundamental types, then you can use the va_start,
va_arg, and va_end macros. Furthermore, if you want to pass a ... list
another function, you can pass the va_list to it. I think it's like this:

void myprintf(const char * format, ...) {
std::cout << "In my printf\n";
va_list ap;
va_start(ap, format);
vprintf(format, va_list);
va_end(ap);
}
 
S

Siemel Naran

David Harmon said:
void va_example(char *format, ...)
{
va_list ap;
char ch;

va_start(ap, format);

/* Note: because of default promotions, you can't use char,
or float types with va_arg. Use int or double instead. */

while ((ch = *format++) != 0) {
if (ch != '%')
fputc(ch, stdout);
else {
if ((ch = *format++) == 0)
break;
switch (ch) {
case 'd': {
int arg = va_arg(ap, int);
char buf[10];
itoa( arg, buf, 10);
fputs( buf, stdout);
break;
}

Out of curiosity, can one use this method to pass class types? In other
words, is

MyClass arg = va_arg(ap, MyClass);

ok?
 
J

Jerry Coffin

[ ... ]
Out of curiosity, can one use this method to pass class types? In other
words, is

MyClass arg = va_arg(ap, MyClass);

ok ?

When you're passing a parameter as part of a variable parameter list,
"If the argument has a non-POD class type (clause 9), the behavior is
undefined." ($5.2.2/7).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top