473,473 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Variable number of arguments in a C++ function

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,

May 18 '06 #1
7 2175
utab wrote:
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

May 18 '06 #2
> Is that possible to give a function a variable number of arguments
[snip]
I googled but all the in formation is in pieces so can you recommend me
a good through reference url for this?


this might help you:
http://www.codeproject.com/cpp/argfunctions.asp

May 18 '06 #3
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,

May 18 '06 #4
dice wrote:
Is that possible to give a function a variable number of arguments

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


this might help you:
http://www.codeproject.com/cpp/argfunctions.asp


But see my post about the downsides to the ellipsis in C++.

Cheers! --M

May 18 '06 #5

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


Yep, that's what I would do.

May 18 '06 #6
utab wrote:
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

May 18 '06 #7
> 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!

May 19 '06 #8

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

Similar topics

2
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg'...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
7
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double...
10
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be...
5
by: Sathyaish | last post by:
I knew this but I have forgotten. How do you declare a function that has to accept a variable number of arguments? For instance, the printf() function has a variable number of arguments it can...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: prasanthag | last post by:
Hi, I am a newbie to this group. I have a problem in handling the variable arguments passed to a function. My requirement is like this. I have 2 functions say, void funcX(int i, int j);...
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
6
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to ...
2
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.