Connecting Tech Pros Worldwide Forums | Help | Site Map

How to write such a function?

tings
Guest
 
Posts: n/a
#1: Jul 23 '05
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!



Artie Gold
Guest
 
Posts: n/a
#2: Jul 23 '05

re: How to write such a function?


tings wrote:[color=blue]
> 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!
>
>[/color]
See, for example:

http://www.gnu.org/software/libc/man...adic%20Example

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 news:comp.lang.c++, the include you should use
is <cstdarg> as opposed to <stdarg.h>.]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/20)
http://www.cafepress.com/goldsays
David Harmon
Guest
 
Posts: n/a
#3: Jul 23 '05

re: How to write such a function?


On Sun, 09 Jan 2005 19:15:07 GMT in comp.lang.c++, "tings"
<tings668@hotmail.com> wrote,[color=blue]
>How to write such a function that can take varible number and tyoes of
>arguments, like printf("... %d, %s...", myInt, myString...)?[/color]

/* 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;
}



Alf P. Steinbach
Guest
 
Posts: n/a
#4: Jul 23 '05

re: How to write such a function?


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

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.

--
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?
Siemel Naran
Guest
 
Posts: n/a
#5: Jul 23 '05

re: How to write such a function?


"tings" <tings668@hotmail.com> wrote in message
news:%ofEd.90206$uM5.33520@bgtnsc05-
[color=blue]
> How to write such a function that can take varible number and tyoes of
> arguments, like printf("... %d, %s...", myInt, myString...)?[/color]

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);
}


Siemel Naran
Guest
 
Posts: n/a
#6: Jul 23 '05

re: How to write such a function?


"David Harmon" <source@netcom.com> wrote in message
[color=blue]
> 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;
> }[/color]

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

MyClass arg = va_arg(ap, MyClass);

ok?


Jerry Coffin
Guest
 
Posts: n/a
#7: Jul 23 '05

re: How to write such a function?


[ ... ]
[color=blue]
> Out of curiosity, can one use this method to pass class types? In[/color]
other[color=blue]
> words, is
>
> MyClass arg = va_arg(ap, MyClass);
>
> ok ?[/color]

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).

--
Later,
Jerry.

The universe is a figment of its own imagination.

Closed Thread