Passing variable parameters to base constructor.

J

jason.cipriani

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
 
V

Victor Bazarov

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
 
J

jason.cipriani

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
 
J

jason.cipriani

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

James Kanze

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

dave_mikesell

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top