473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Variable arguments for a Constructor

Dear all,

How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?

I know that there is a way to make functions take variable number of
arguments, but that is not recommended. If I can not find another way,
I will apply that one.

Regards

Aug 26 '06 #1
16 8324
utab wrote:
Dear all,

How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?

I know that there is a way to make functions take variable number of
arguments, but that is not recommended. If I can not find another way,
I will apply that one.

Regards
There is the usual way of adding ...
I don't know why it is not recommended, but you can always use
something of a list.
Even and STL::list, that contains structures like this:
struct PARAMETER {
PARAM_TYPE type;
void *data;
};

Aug 26 '06 #2
struct PARAMETER {
PARAM_TYPE type;
void *data;
};
Thank you but Can you give an example with this structure? I am a
little bit slow learner ;)

Aug 26 '06 #3
utab wrote:
How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?

I know that there is a way to make functions take variable number of
arguments, but that is not recommended. If I can not find another way,
I will apply that one.
You can't use boost::tuple, because the tuple still needs to know how
many arguments there are. There may be some complicated way of using
template deduction to get around that problem, but it would probably be
more trouble than it's worth.

There are two recommended ways to accomplish what you want. The first
is to simply overload the constructor so that you have defined a
constructor for every possible number and type of arguments you intend
to use. The second is to pass one of the standard library's containers
(e.g., std::list, std::vector, etc) as as argument, and have that
container include all the arguments.

Best regards,

Tom

Aug 26 '06 #4
utab schrieb:
Dear all,

How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?

I know that there is a way to make functions take variable number of
arguments, but that is not recommended. If I can not find another way,
I will apply that one.

Regards
Maybe you could say what you _want_ to do, not how you think it should
be done. Maybe another design, or another C++ trick, would be more
appropriate. Otherwise, supplying a data-container (list, or some
simple data-only struct) would help. Also boost named parameters may be
appropriate, though I dislike them for their complexity. Tuples won't
help, as they have a fixed layout, and if you can find appropriate
default parameters for unsupplied arguments, you can do the same with
the constructor, or with simple structs (don't add depencies just
because it looks cool...).
best regards,
-- Markus
Aug 26 '06 #5

"gMorphus" <gu*****@gmail.comskrev i meddelandet
news:11**********************@m79g2000cwm.googlegr oups.com...
utab wrote:
>Dear all,

How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?

I know that there is a way to make functions take variable number
of
arguments, but that is not recommended. If I can not find another
way,
I will apply that one.

Regards

There is the usual way of adding ...
I don't know why it is not recommended, but you can always use
something of a list.
It's more that it is a C-ism, what yo have to do when you can't
overload. A C++ class can have any number of constructors, with
different parameter types. They can even be templated, if you don't
know the types before hand.

Also, you can only pass PODs to ... anyway, so its usability is
limited.
Bo Persson
Aug 26 '06 #6
utab wrote:
>
I know that there is a way to make functions take variable number of
arguments, but that is not recommended.
If it's the appropriate tool, use it. Far too many programmers spend far
too much time developing elaborate workarounds for things that someone
has told them are bad.
Aug 26 '06 #7

utab wrote:
struct PARAMETER {
PARAM_TYPE type;
void *data;
};

Thank you but Can you give an example with this structure? I am a
little bit slow learner ;)
It's not too generic.. You can use the RTTI mechanism to identify your
types, or you can use some enum you create for specific types you want
to allow:

enum PARAM_TYPE {
INT_TYPE,
SHORT_TYPE,
DOUBLE_TYPE,
STRING_TYPE
};

and so on...

struct PARAMETER {
PARAM_TYPE type;
void *data;
};

than define a list type:
typedef std::list < PARAMETER t_ParametersList;

class MyClass {
public:
MyClass ( t_ParametersList &params );
....
};

now you go over the list, using the type member of the struct you know
how to case the data member of it.

Aug 26 '06 #8

Thomas Tutone wrote:
utab wrote:
How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?

I know that there is a way to make functions take variable number of
arguments, but that is not recommended. If I can not find another way,
I will apply that one.

You can't use boost::tuple, because the tuple still needs to know how
many arguments there are. There may be some complicated way of using
template deduction to get around that problem, but it would probably be
more trouble than it's worth.

There are two recommended ways to accomplish what you want. The first
is to simply overload the constructor so that you have defined a
constructor for every possible number and type of arguments you intend
to use.
For example:

class Foo {
public:
Foo();
Foo(int i);
Foo(int i, int j);
Foo(int i1, int i2, int i3);
Foo(std::string s1, std::string s2);
Foo(std::string s1, std::string s2, std::string s3);
// etc. ...
};
The second is to pass one of the standard library's containers
(e.g., std::list, std::vector, etc) as as argument, and have that
container include all the arguments.
For example:

class Bar {
public:
Bar(const std::vector<int>& v);
};

std::vector<intvec;
vec.push_back(1);
vec.push_back(5);
vec.push_back(-32);
Bar bar(vec);

Hope that helps.

Best regards,

Tom

Aug 26 '06 #9
You can't use boost::tuple, because the tuple still needs to know how
many arguments there are. There may be some complicated way of using
template deduction to get around that problem, but it would probably be
more trouble than it's worth.
I got that
There are two recommended ways to accomplish what you want. The first
is to simply overload the constructor so that you have defined a
constructor for every possible number and type of arguments you intend
to use. The second is to pass one of the standard library's containers
I will supply the number of parameters first and then the parameters
must be pushed_back into a vector of parameters. sth like

ctor(int num_p,p1,p2,....)

for(int i=0;i!=num_p;++i)
params_vec.push_back(parameters)

params_vec is a data member of the class. Is there a way to get around
this.

Aug 26 '06 #10

class Foo {
public:
Foo();
Foo(int i);
Foo(int i, int j);
Foo(int i1, int i2, int i3);
Foo(std::string s1, std::string s2);
Foo(std::string s1, std::string s2, std::string s3);
// etc. ...
This is not practical, I would like a more automated way
For example:

class Bar {
public:
Bar(const std::vector<int>& v);
};

std::vector<intvec;
vec.push_back(1);
vec.push_back(5);
vec.push_back(-32);
Bar bar(vec);

Hope that helps.

Best regards,

Tom
This is another non automated way. Please see my post on what I try to
do on the ctor.

Regards,

Aug 26 '06 #11
utab posted:
How can I generate a constructor to take a variable number of
arguments.
The same way with any other function:

#include <cassert>
#include <cstdarg>
#include <iostream>

using std::cout;

class MyClass {
public:

MyClass(char const *p,...)
{
assert(p); assert(*p);

va_list ap;
va_start(ap,p);

do
{
assert(*p == 'u' || *p == 'i' || *p == 'd' || *p == 's');

switch(*p)
{
case 'u': cout << va_arg(ap,unsigned) << '\n'; break;
case 'i': cout << va_arg(ap,int) << '\n'; break;
case 'd': cout << va_arg(ap,double) << '\n'; break;
case 's': cout << va_arg(ap,char const*) << '\n'; break;
}
}
while(*++p);
}
};

int main()
{
MyClass obj("isduds",-77,"Hello!",23948.352,88U,23.3,"Goodbye!");
}

--

Frederick Gotham
Aug 26 '06 #12
utab schrieb:
[...]

I will supply the number of parameters first and then the parameters
must be pushed_back into a vector of parameters. sth like

ctor(int num_p,p1,p2,....)

for(int i=0;i!=num_p;++i)
params_vec.push_back(parameters)

params_vec is a data member of the class. Is there a way to get around
this.
Yes, you pass params_vec to the constructor.

ctor(const std::vector<ParamType&params) : params_vec(params)
{}

ParamType must have a copy constructor. Please see also

http://www.parashift.com/c++-faq-lite/ctors.html
best regards,
-- Markus
Aug 26 '06 #13
Thomas Tutone wrote:
You can't use boost::tuple, because the tuple still needs to know how
many arguments there are. There may be some complicated way of using
template deduction to get around that problem, but it would probably be
more trouble than it's worth.
It's not complicated:

#include <vector>
#include <boost/tuple/tuple.hpp>

using namespace boost::tuples;
using namespace std;

struct X
{
template< typename Tuple >
X(Tuple const& tuple)
{
init(tuple);
}

private:
template< typename Head, typename Tail >
void init(cons< Head, Tail const& cell)
{
m_ints.push_back(cell.get_head());

init(cell.get_tail());
}

void init(null_type null) { }

vector< int m_ints;
};

int main()
{
X x = make_tuple(1, 2, 3);
}

It's not always the right thing to do, admittedly.

Jens
Aug 26 '06 #14
On Sat, 26 Aug 2006 17:23:52 +0200, Markus Grueneis
<gr********@gmx.netwrote:
>utab schrieb:
>I will supply the number of parameters first and then the parameters
must be pushed_back into a vector of parameters. sth like

ctor(int num_p,p1,p2,....)

for(int i=0;i!=num_p;++i)
params_vec.push_back(parameters)

params_vec is a data member of the class. Is there a way to get around
this.

Yes, you pass params_vec to the constructor.

ctor(const std::vector<ParamType&params) : params_vec(params)
{}
The 'STL-way' is to pass iterators, e.g.

template <class Iter>
ctor (Iter begin, Iter end): params_vec (begin, end){}

Best wishes,
Roland Pibinger
Aug 26 '06 #15
Roland Pibinger schrieb:
On Sat, 26 Aug 2006 17:23:52 +0200, Markus Grueneis
<gr********@gmx.netwrote:
>utab schrieb:
>>I will supply the number of parameters first and then the parameters
must be pushed_back into a vector of parameters. sth like

ctor(int num_p,p1,p2,....)

for(int i=0;i!=num_p;++i)
params_vec.push_back(parameters)

params_vec is a data member of the class. Is there a way to get around
this.
Yes, you pass params_vec to the constructor.

ctor(const std::vector<ParamType&params) : params_vec(params)
{}

The 'STL-way' is to pass iterators, e.g.

template <class Iter>
ctor (Iter begin, Iter end): params_vec (begin, end){}
Thx, now I remember it.

Just for the OP:
This allows to pass ranges, not just whole collections. Additionally,
you can pass a range of vector<Tto the constructor of vector<Qat
T != Q, *iff* Q can be converted to T.
thx,
-- Markus
Best wishes,
Roland Pibinger
Aug 26 '06 #16

utab wrote:
Dear all,

How can I generate a constructor to take a variable number of
arguments. Is boost tuple appropriate for this?

I know that there is a way to make functions take variable number of
arguments, but that is not recommended. If I can not find another way,
I will apply that one.

Regards
what about the "named parameter idiom" described in the FAQ?

Diego Martins

Aug 28 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: zimba | last post by:
Hello, Do you know a better way to : - instanciate a class with an unknown number of parameters in the constructor - call an object's method, also with an unknown number of parameters ? ...
4
by: Piotr Sawuk | last post by:
Hello, I'm new in this group and new to c++ programming. And I already have my first question which wasn't answered by any text-book on c++ programming I have seen so-far: How can I define a...
5
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
5
by: greenflame | last post by:
How can I tell if a variable. I want ot tell whether two variables are: 1. Both numbers. Just a number. same as the number object in javascript. 2. Either one is a string containing a number....
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
3
by: Mark Berry | last post by:
Hi, I'm moving from VB6 to VB 2005. Two questions: 1. One book I'm using has syntax like Dim testConnection as SqlConnection = New SqlConnection(connectionString) However, the Help for...
1
by: tbandrow | last post by:
I would like to get this to compile: public class GenericCtor<T> where T: class, new() { public static T MakeIt() { return new T(1, 2); } }
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
1
by: mavigozler | last post by:
Is there a strict requirement for having the same number of parameters in a call to a function and/or class method---in this case a class constructor---and the definition of the function and/or...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.