How to call a virtual base function with variable parameters?

Discussion in 'C++' started by rbfish@hotmail.com, Jan 3, 2006.

  1. Guest

    Hi,

    How can I call a virtual base function with variable parameters? like,

    class base
    {
    public:
    int printf(const char *fmt, ...)
    {
    // do anything here.
    }
    };

    class derived : public base
    {
    public:
    int printf(const char *fmt, ...)
    {
    // do something here.
    ...
    // then call base function
    return base::printf(fmt, XXX); // WHAT CAN I WRITE HERE?
    }
    };

    Thanks in advance,
    rbfish
    , Jan 3, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hi,
    >
    > How can I call a virtual base function with variable parameters? like,
    >
    > class base
    > {
    > public:
    > int printf(const char *fmt, ...)
    > {
    > // do anything here.
    > }
    > };
    >
    > class derived : public base
    > {
    > public:
    > int printf(const char *fmt, ...)
    > {
    > // do something here.
    > ...
    > // then call base function
    > return base::printf(fmt, XXX); // WHAT CAN I WRITE HERE?
    > }
    > };
    >
    > Thanks in advance,
    > rbfish


    You'll need to use the <cstdarg> facilities. See your favorite C text
    book for more, since C++ discourages using variable args. (A C++
    approach would be to use iostreams, which, unlike printf and its
    ellipses, are type-safe.)

    Cheers! --M
    mlimber, Jan 3, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > Hi,
    >
    > How can I call a virtual base function with variable parameters? like,
    >
    > class base
    > {
    > public:
    > int printf(const char *fmt, ...)
    > {
    > // do anything here.
    > }
    > };
    >
    > class derived : public base
    > {
    > public:
    > int printf(const char *fmt, ...)
    > {
    > // do something here.
    > ...
    > // then call base function
    > return base::printf(fmt, XXX); // WHAT CAN I WRITE HERE?
    > }
    > };


    You can't. It's as simple as that. For ellipsis arguments, the compiler
    still
    needs to know the exact arguments at the call site, even if the
    function
    called doesn't know them at compile time.

    In this case, derived::printf doesn't have the arguments available at
    compile
    time to call base::printf. Have a look at more modern solutions like
    iostream
    or boost::format.

    HTH,
    Michiel Salters
    , Jan 4, 2006
    #3
  4. Guest

    I just used printf() as an example. Actually I liked to use it in the
    other way.
    Anyway, thank you very much.
    , Jan 4, 2006
    #4
  5. Mike Smith Guest

    wrote:
    > I just used printf() as an example. Actually I liked to use it in the
    > other way.


    What other way? The << and >> operators can still be used to pass
    parameter lists of variable length in a more "C++-ish" way than vararg.

    --
    Mike Smith
    Mike Smith, Jan 5, 2006
    #5
  6. Earl Purple Guest

    wrote:
    > I just used printf() as an example. Actually I liked to use it in the
    > other way.
    > Anyway, thank you very much.


    Well your function above isn't even described as virtual but you could
    have a non-virtual method in the base-class that uses ... which then
    expands to var_args and calls a virtual method. Something like;

    class base
    {
    public:
    void print( const std::string & format, ... )
    {
    va_list args;
    va_begin( args, format ); // or whatever it is
    do_print( format, args );
    }
    protected:
    virtual void do_print( const std::string & format, va_list args );
    };

    A better way to print a variable argument list though is to get the
    "variable" parameter to be some kind of collection. You can use an
    operator like + between the arguments.
    Earl Purple, Jan 5, 2006
    #6
    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.

Share This Page