How to Accept Variable number of arguments ?

V

vivekian

Trying to write a function which can accept variable number of
arguments of the same data type , ranging from 1 ... n . What would be
the best way to go about this.

Thanks,
vivekian
 
M

meagar

If all the arguments are going to be of the same type, the easiest way
all around is just to pass in a vector of that type. You'll then be
able to iterate over all the inputs. If whatever you're doing can be
generalized, you could template the function to take a vector<T> too.
 
B

Brent Ritchie

I believe the elipses operator is what you want here. printf uses it.

void someMethod(int number_of_arguments, ...)
{

}

This will allow you to take any number of arguments, of any type. Beware it
can easily be abused, and should be used with caution (the elipses operator,
not printf.)
 
J

John Harrison

vivekian said:
Trying to write a function which can accept variable number of
arguments of the same data type , ranging from 1 ... n . What would be
the best way to go about this.

One way is to ose the va_* macros declared in <stdarg.h>.

It's impossible to say what the best way is unless you tell us what kind
of function you are trying to write. It all depends on exactly what you
are trying to do.

john
 
V

vivekian

The arguments passed will be a set of integers ( the size of the set is
variable ). Each argument will be assigned to a node in a linked list (
this is what the function does basically ). So the size of the linked
list depends on the number of arguments passed. I think the method
specified by Meager sounds good, though if there is a better way do let
know.

Thanks again,
vivekian
 
M

Michiel.Salters

vivekian said:
The arguments passed will be a set of integers ( the size of the set is
variable ). Each argument will be assigned to a node in a linked list (
this is what the function does basically ). So the size of the linked
list depends on the number of arguments passed. I think the method
specified by Meager sounds good, though if there is a better way do let
know.

You don't need a function for that! The constructor of std::list< >
already
does that.

HTH,
Michiel Salters
 
R

Richard Herring

In message said:
The arguments passed will be a set of integers ( the size of the set is
variable ). Each argument will be assigned to a node in a linked list (
this is what the function does basically ). So the size of the linked
list depends on the number of arguments passed. I think the method
specified by Meager sounds good, though if there is a better way do let
know.
If you're adding elements one at a time, you might consider overloading
operator<<:

// warning - incomplete skeleton code for illustration only
class MyType {
public:
void add(int i);
// etc...
};

MyType & operator<<(MyType & obj, int arg)
{
obj.add(arg);
return obj;
}

MyType x;
x << arg1 << arg2 << arg3; // etc

If your set of arguments constitute some kind of a sequence, you could
have a templated function that takes two iterators representing the
first and one-beyond-the end elements. This is similar to meagar's
solution, but more flexible, since you aren't constrained to pass a
vector, but can use any kind of representation that behaves like an
input iterator - for example you could use an istream_iterator to read
the data from a file.

template <typename Iterator>
void AddElements(MyType & obj, Iterator p, Iterator end)
{
for (; p!=end; ++p)
obj.add(*p);
}

MyType x;
AddElements(x, istream_iterator<int>(cin), istream_iterator<int>());

In this case you could alternatively make this a member function of
MyType and omit the first argument.
 
V

vivekian

Thanks for all the solutions . Think Richards solution suits my problem
the best and works well.

vivekian
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top