How to write such a function?

Discussion in 'C++' started by tings, Jan 9, 2005.

  1. tings

    tings Guest

    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!
    tings, Jan 9, 2005
    #1
    1. Advertising

  2. tings

    Artie Gold Guest

    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/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 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
    Artie Gold, Jan 9, 2005
    #2
    1. Advertising

  3. tings

    David Harmon Guest

    On Sun, 09 Jan 2005 19:15:07 GMT in comp.lang.c++, "tings"
    <> 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;
    }
    David Harmon, Jan 9, 2005
    #3
  4. * 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?
    Alf P. Steinbach, Jan 9, 2005
    #4
  5. tings

    Siemel Naran Guest

    "tings" <> 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);
    }
    Siemel Naran, Jan 9, 2005
    #5
  6. tings

    Siemel Naran Guest

    "David Harmon" <> 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?
    Siemel Naran, Jan 9, 2005
    #6
  7. tings

    Jerry Coffin Guest

    [ ... ]

    > 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.
    Jerry Coffin, Jan 9, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. komal
    Replies:
    6
    Views:
    1,405
    msalters
    Jan 25, 2005
  2. Chuck Amadi
    Replies:
    0
    Views:
    510
    Chuck Amadi
    Jun 22, 2004
  3. Replies:
    6
    Views:
    1,546
    David Bolen
    Mar 1, 2005
  4. Replies:
    7
    Views:
    364
    CBFalconer
    Mar 25, 2006
  5. vinay
    Replies:
    12
    Views:
    1,980
    Athel Cornish-Bowden
    Aug 19, 2009
Loading...

Share This Page