Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing variable parameters to base constructor.

Reply
Thread Tools

Passing variable parameters to base constructor.

 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-25-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 5:48 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 6:52 pm, "(E-Mail Removed)"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-26-2008
On Feb 25, 11:48 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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:(E-Mail Removed)
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
 
Reply With Quote
 
dave_mikesell@fastmail.fm
Guest
Posts: n/a
 
      02-26-2008
On Feb 26, 4:03*am, James Kanze <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameters to an executable vs. passing them to a server Ramon F Herrera C++ 8 09-13-2009 02:48 AM
Passing all parameters to a method with a unique variable Mario Ruiz Ruby 9 11-06-2007 12:45 PM
Passing derived classes uninitialized variable to base class constructor !! PSN C++ 2 09-22-2006 04:14 PM
How to call a virtual base function with variable parameters? rbfish@hotmail.com C++ 5 01-05-2006 06:18 PM
getting and passing variable number of parameters Michael Cohen C Programming 2 12-26-2003 12:14 AM



Advertisments