Functions with variable argument count..

This is a discussion on Functions with variable argument count.. within the C++ Programming forums, part of the General Programming Boards category; I'm wondering if there is a way to deal with functions that can have a variable number of arguments passed ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Functions with variable argument count..

    I'm wondering if there is a way to deal with functions that can have a variable number of arguments passed to it. I already know of va_* functions but it doesn't seem that good. I was hoping there would be some way to restrict the data type to one data type, and also have random access to the arguments (va_* doesn't seem to have random access, unless you iterate through and copy it all to your own array first). It also seems that the va_* only works for basic data types and not my own class that i made.

    Something like this:

    Code:
    std::string FormatString(const std::string& format, int num, const Variable* varList, ...)
    {
    }
    Where "num" is the number of arguments passed and "varList" is an array of the arguments.

    EDIT: err...nvm about the example, but is there any other methods for variable functions arguments? Would you have to use asm or something?
    Last edited by 39ster; 04-11-2009 at 10:06 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by 39ster
    I was hoping there would be some way to restrict the data type to one data type, and also have random access to the arguments
    You could make the parameter a std::vector.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I suppose i could. Anyways a little bit more looking around and i found out that variable arguments are alot more restrictive than i thought. I thought the program would pass "int num" but it turns out you have to pass that manually =D

    It would be cool if the example i gave could be possible though (as in, "varList" holds all the arguments proceeding "const std::string& format" and "int num" would the amount of elements in varList)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by 39ster
    It would be cool if the example i gave could be possible though (as in, "varList" holds all the arguments proceeding "const std::string& format" and "int num" would the amount of elements in varList)
    Take a look at how Boost.Format does it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable Array Size From Command Line Argument?
    By Cell in forum C Programming
    Replies: 6
    Last Post: 03-30-2009, 10:08 PM
  2. variable modification inside functions
    By point in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2005, 09:36 PM
  3. variable argument lists
    By cProGrammer28 in forum C Programming
    Replies: 2
    Last Post: 05-03-2005, 07:27 AM
  4. Variable Argument Functions
    By genghis in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2002, 02:01 PM
  5. Overloading functions with variable arguments
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-08-2001, 09:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21