Passing variable parameters to base constructor.

Discussion in 'C++' started by jason.cipriani@gmail.com, Feb 25, 2008.

  1. Guest

    If I have a base class with a constructor that takes '...' parameters,
    and I want to derive a class from that and give it a similar
    constructor and have it pass the variable parameters to the base, is
    there a way to do that?

    For example:

    class Base {
    public:
    Base (const char *str, ...);
    };

    class Derived : public Base {
    public:
    Derived (const char *str, ...);
    };

    I want to have Derived's constructor call Base's constructor, passing
    all the optional parameters. Right now I am splitting things up into a
    separate initialization function that takes a va_list, but I'd really
    like to avoid having to separate it out:

    class Base {
    public:
    Base (void);
    Base (const char *str, ...); // <-- this would call Init()
    protected:
    void Init (va_list args);
    };

    class Derived : public Base {
    public:
    Derived (const char *str, ...) {
    // call's Base Init with va_list...
    va_list args;
    va_start(args, str);
    Init(args);
    va_end(args);
    }
    };

    Thanks,
    Jason
    , Feb 25, 2008
    #1
    1. Advertising

  2. wrote:
    > If I have a base class with a constructor that takes '...' parameters,
    > and I want to derive a class from that and give it a similar
    > constructor and have it pass the variable parameters to the base, is
    > there a way to do that?


    No. You would need a base class c-tor that takes 'va_list'.

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 25, 2008
    #2
    1. Advertising

  3. Guest

    On Feb 25, 5:48 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > If I have a base class with a constructor that takes '...' parameters,
    > > and I want to derive a class from that and give it a similar
    > > constructor and have it pass the variable parameters to the base, is
    > > there a way to do that?

    >
    > No. You would need a base class c-tor that takes 'va_list'.


    Is there some clean way to initialize a va_list from the derived class
    constructors parameters so it can be passed to a base constructor?
    Based on that advice I got this working, at least on this machine
    (where va_start() evaluates to something):


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

    class Base {
    public:
    Base (const char *msg, va_list args) {
    vprintf(msg, args);
    }
    };

    class Derived : public Base {
    public:

    Derived (const char *msg, ...)
    : Base(msg, (va_start(m_args, msg), m_args) )
    {
    va_end(m_args);
    }

    private:
    va_list m_args;
    };

    int main (int argc, char **argv) {
    Derived d("test %i %i\n", 1, 2);
    return 0;
    }


    But it's really weird looking; and I'm not sure if I like it better
    than having separate functions. Also if va_start is #defined to
    something inside {}'s then that wouldn't compile any more.

    Thanks,
    Jason
    , Feb 25, 2008
    #3
  4. Guest

    On Feb 25, 6:52 pm, ""
    <> wrote:
    > But it's really weird looking; and I'm not sure if I like it better
    > than having separate functions. Also if va_start is #defined to
    > something inside {}'s then that wouldn't compile any more.


    I guess it also would break if va_list needed some initialization,
    since m_args is not initialized yet when it's passed to va_start there.
    , Feb 25, 2008
    #4
  5. James Kanze Guest

    On Feb 25, 11:48 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > If I have a base class with a constructor that takes '...' parameters,
    > > and I want to derive a class from that and give it a similar
    > > constructor and have it pass the variable parameters to the base, is
    > > there a way to do that?


    > No. You would need a base class c-tor that takes 'va_list'.


    And even then, you can't, since there's no way to get the
    va_list without a declaration, which is impossible in an
    initializer list. You need a base class with a default
    constructor and an initialize function which takes a va_list
    (which results in the dreaded two phase initialization).

    My own opinion is that va_args are there for reasons of C
    compatilibity, and nothing else, and shouldn't be used in C++
    code. There are almost always better alternatives.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Feb 26, 2008
    #5
  6. Guest

    On Feb 26, 4:03 am, James Kanze <> wrote:

    > My own opinion is that va_args are there for reasons of C
    > compatilibity, and nothing else, and shouldn't be used in C++
    > code.  There are almost always better alternatives.


    Indeed.

    To the OP - what problem are you trying to solve with va_args?
    , Feb 26, 2008
    #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