Variable number of arguments in a C++ function

U

utab

Dear all,

Is that possible to give a function a variable number of arguments and
how, I know there is a way? and these arguments may differ such as
vectors strings ans so on...

Lets say

if I supply
f(),
f(x,y,z)
f(x,y,z,k)
f(x,y,z,k,l,....)

everytime I use the function I will be able to provide as much as
arguments as I can.

I googled but all the in formation is in pieces so can you recommend me
a good through reference url for this?

Regards,
 
M

mlimber

utab said:
Dear all,

Is that possible to give a function a variable number of arguments and
how, I know there is a way? and these arguments may differ such as
vectors strings ans so on...

Lets say

if I supply
f(),
f(x,y,z)
f(x,y,z,k)
f(x,y,z,k,l,....)

everytime I use the function I will be able to provide as much as
arguments as I can.

I googled but all the in formation is in pieces so can you recommend me
a good through reference url for this?

Regards,

There is the elipsis, which would make your function like printf, but
using it is very much discouraged in C++ because it is not type-safe
and you can't pass non-built-in types (at least without lots of ugly
casting). What problem is it that you are trying to solve? Perhaps we
can suggest an alternate approach (overloading, method chaining,
boost::bind, etc.) that would yield the same result.

Cheers! --M
 
U

utab

Hi thx,

I am trying to implement a class which will read some fields, process
them and then replace the old fields with the newer fields in brief.

If my function is Read_Data() for example I would like to read some
fields that I know in advance. To clarify the situation, Take a look at
the below example,

field1 / ..../..../..../..../..... / ..../ filed 10

In a dat file I have 10 fileds of 8 characters. And if I want to read
multiple fields, I want to be able to give them as parameters, such as
field1, field4 ,field9 ...

Sometimes I would need to read 3 fileds and maybe sometimes all.
One way to to this maybe can be to push ,the whole line by parsing into
8 characters, onto a vector

But still fuzzy to me, maybe I have think on that some more time.

Regards,
 
M

mlimber

utab said:
Hi thx,

I am trying to implement a class which will read some fields, process
them and then replace the old fields with the newer fields in brief.

If my function is Read_Data() for example I would like to read some
fields that I know in advance. To clarify the situation, Take a look at
the below example,

field1 / ..../..../..../..../..... / ..../ filed 10

In a dat file I have 10 fileds of 8 characters. And if I want to read
multiple fields, I want to be able to give them as parameters, such as
field1, field4 ,field9 ...

Sometimes I would need to read 3 fileds and maybe sometimes all.
One way to to this maybe can be to push ,the whole line by parsing into
8 characters, onto a vector

But still fuzzy to me, maybe I have think on that some more time.

Regards,

Ok, and where would the variable argument function come in?

Cheers! --M
 
A

asterisc

everytime I use the function I will be able to provide as much as
arguments as I can.

Why don't u send a std::vector<std::string> as a parameter?
You can store "an arbitrary" number of elements in there!
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top