Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to write such a function?

Reply
Thread Tools

How to write such a function?

 
 
tings
Guest
Posts: n/a
 
      01-09-2005
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!


 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      01-09-2005
tings wrote:
> 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/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
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      01-09-2005
On Sun, 09 Jan 2005 19:15:07 GMT in comp.lang.c++, "tings"
<(E-Mail Removed)> wrote,
>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;
}



 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-09-2005
* 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.

--
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?
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      01-09-2005
"tings" <(E-Mail Removed)> wrote in message
news:%ofEd.90206$uM5.33520@bgtnsc05-

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


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      01-09-2005
"David Harmon" <(E-Mail Removed)> wrote in message

> 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?


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      01-09-2005
[ ... ]

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

--
Later,
Jerry.

The universe is a figment of its own imagination.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
is it possible to write such a macro? fei.liu@gmail.com C Programming 7 03-25-2006 12:05 AM
cannot open file in write mode, no such file or directory haynesc@gmail.com Python 6 03-01-2005 04:17 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments