4

I have th following code:

class A{

    //Constructor
    public: A(int count,...){
        va_list vl;
        va_start(vl,count);
        for(int i=0;i<count;i++)
            /*Do Something ... */
        va_end(vl);
    }
};

class B : public A{

    //Constructor should pass on args to parent
    public: B(int count,...) : A(int count, ????)
    {}
};

How can I do that?

Note: I would prefer to have call the constructor in the initialization list and not in the constructor body. But if this is the only way, I am also interested to hear how this works!

Thanks

Johannes Gerer
  • 25,508
  • 5
  • 29
  • 35
  • 2
    What compiler are you using? You want [the new C++0x initializer lists](http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists). – Ben Voigt May 30 '11 at 18:58
  • He wants a non-sucky initializer, if you ask me- ahem. More importantly, he wants constructor forwarding or variadic template.s – Puppy May 30 '11 at 18:59
  • Several duplicates, one of them: http://stackoverflow.com/questions/205529/c-c-passing-variable-number-of-arguments-around – Kiril Kirov May 30 '11 at 19:01
  • @Kiril: It's no duplicate. I can't use va_list in the initialization inline list. Why wasting everybodies time (including yours) with such comments? – Johannes Gerer May 30 '11 at 19:07
  • @Ben: I dont want C++0x. I want the old standard's initialization list. – Johannes Gerer May 30 '11 at 19:07
  • @user578832: I said "initializer list" and I meant *initializer list*. They're a new feature of C++0x, different from the *ctor-initializer* which I think is what you are referring to as an "initialization list". Nothing in this question gave any clue to the fact that you can't use C++0x, so I suggested the best solution assuming you could. – Ben Voigt May 30 '11 at 19:34

1 Answers1

3

You cannot forward on to an ellipsis. The second constructor will have to take a va_list, I think it is.

This would be possible with C++0x's base constructor forwarding, or variadic templates.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Those are great when the base class has a fixed number of parameters, unknown to you. Variadic arguments are something else, and are handled (and easily forwarded) by `std::initializer_list`. – Ben Voigt May 30 '11 at 19:00
  • Actually, if the parameters aren't all the same type, you would need a variadic template, wouldn't you? – Ben Voigt May 30 '11 at 19:35
  • @Ben: Yea. You can inherit base constructors though, which really settles the whole thing. `struct b { b(/* anything */); } struct d : b { using b::b; }; // le done` – GManNickG May 30 '11 at 20:07
  • Except that, at least for g++, base constructors with C-style variadic arguments are simply dropped: https://gcc.gnu.org/ml/gcc-patches/2012-10/msg02294.html – jwoolard Oct 11 '16 at 10:30