passing undefined number of parameters (function & constructor)

D

david

Hello,
I looking for a way how I would be able to pass multiple parameters to
function and constructor. For functions there is one that that works
in C and C++. That would be using va_list, va_start, va_end and va_arg
macros as I remember. But then trying to use it in constructor it will
return only one parameter, the first one. Is this the only way how you
can pass multiple parameters or there is another one? Maybe
constructor has a bit stricter syntax?

david
 
V

Victor Bazarov

david said:
I looking for a way how I would be able to pass multiple parameters to
function and constructor. For functions there is one that that works
in C and C++. That would be using va_list, va_start, va_end and va_arg
macros as I remember. But then trying to use it in constructor it will
return only one parameter, the first one. Is this the only way how you
can pass multiple parameters or there is another one? Maybe
constructor has a bit stricter syntax?

Please see FAQ section 5, especially question 5.8. The only essential
difference between c-tors and regular functions is that c-tors have the
initialiser lists (which you don't have to use). When the body of
a c-tor is entered, everything is like in a regular function. If you
have problems with some code, make sure to follow the recommendations
of the FAQ 5.8.

V
 
P

Peter

Hello,
I looking for a way how I would be able to pass multiple parameters to
function and constructor. For functions there is one that that works
in C and C++. That would be using va_list, va_start, va_end and va_arg
macros as I remember. But then trying to use it in constructor it will
return only one parameter, the first one. Is this the only way how you
can pass multiple parameters or there is another one? Maybe
constructor has a bit stricter syntax?

david

use std::list<boost::any> as the only argument to the constructor.
See also
http://www.boost.org/doc/html/any.html

e.g.
#include <list>
#include <boost/any.hpp>
#include <string>

struct A
{ ...
A(const std::list<boost::any> &_r);

};


int main(int argc, char **argv)
{ std::list<boost::any> s;
s.push_back(1); //push an integer
s.push_back(1.0);// push a double
s.push_back("test"); // push a const char *
s.push_back(std::string("test")); // push a std::string

A s1(s); // call the constructor
}
 
D

david

Thanks for the help, but writing small example I figured out what
mistake I was doing. Actually it is really stupid one. I even told a
few people how it works, but only now I noticed that the first
parameter was wrong. The first parameter should be the number of
following parameters. like House new_house(7, 1,2,3,4,5,6,7);

Tip: It took me some time to find FAQ, it would be better if it had
some special page in this group.
 
V

Victor Bazarov

david said:
[..]
Tip: It took me some time to find FAQ, it would be better if it had
some special page in this group.

Not sure what you mean. Are you using Usenet or Google's interface
to it? Do you know what Usenet is? There is no "page" on Usenet.

V
 
J

James Kanze

Please see FAQ section 5, especially question 5.8. The only essential
difference between c-tors and regular functions is that c-tors have the
initialiser lists (which you don't have to use). When the body of
a c-tor is entered, everything is like in a regular function.

That's an important difference here, however, since there is no
way to access the variable arguments in the initialiser list.

Of course, most of the time, the variable arguments will be used
to fill a vector, or some such, so you won't have to access them
in the initializer list.

(For production code, C++ generally offers better and safer
solutions that variable argument lists.)
 
T

Tim H

For a safer alternative to varargs, see another thread called "type-
safe varargs" (or something like that) where I posted a snippet. It's
not ideal, but sometimes, it REALLY is what you need.

Tim
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top