Variable number of arguments in a C++ function

Discussion in 'C++' started by utab, May 18, 2006.

  1. utab

    utab Guest

    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,
    utab, May 18, 2006
    #1
    1. Advertising

  2. utab

    mlimber Guest

    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
    mlimber, May 18, 2006
    #2
    1. Advertising

  3. utab

    dice Guest

    > 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
    dice, May 18, 2006
    #3
  4. utab

    utab Guest

    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,
    utab, May 18, 2006
    #4
  5. utab

    mlimber Guest

    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
    mlimber, May 18, 2006
    #5
  6. utab

    Noah Roberts Guest

    utab wrote:

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


    Yep, that's what I would do.
    Noah Roberts, May 18, 2006
    #6
  7. utab

    mlimber Guest

    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
    mlimber, May 18, 2006
    #7
  8. utab

    asterisc Guest

    > 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!
    asterisc, May 19, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    1
    Views:
    368
    Ron Natalie
    Oct 16, 2003
  2. Vince
    Replies:
    3
    Views:
    375
    Victor Bazarov
    Nov 10, 2004
  3. jmborr
    Replies:
    1
    Views:
    389
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    798
  5. Navaneeth
    Replies:
    4
    Views:
    536
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page