Thread: overloading for an arbitry number of arguments in a function

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    overloading for an arbitry number of arguments in a function

    I'm sure I've seen the answer around and plum forgot about it, but is there a way to say, overload a function number of arguments that I may not know until runtime. The arguments would be of the same type, but it could be two or three or n sized. I ususally just have a vector plugged in to the parameter and that's that. But just curios.

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    You may be thinking of this:
    Code:
       func(int a, in b, ...)
    This is not legal:
    Code:
       func( ...)

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, by using the ... syntax:
    Code:
    void foo(T first, ...)
    Takes a variable number of arguments. You need to use some special code to fetch the argument. There are:
    va_start
    va_end
    va_list
    va_arg

    Code:
    void foo(int first, ...)
    {
    	va_list list;
    	va_start(list, &first);
    
    	int n = va_arg(list, int);
    	double d = va_arg(list, double);
    	char* p = va_arg(list, char*);
    
    	va_end(list);
    }
    Last edited by Elysia; 01-25-2008 at 08:57 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    ah, very messy looking, I'll just stick to what I was doing originally, but thanks for the info, just curious.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, also, since it's variable, there's no way to tell the end of the list.
    As you may have noticed, if you use functions such as sprintf or things and specify more type specifiers in the format list than arguments you pass, you get junk data because it doesn't know you only passed X arguments.
    The usual way is simply to pass along some sort of "end" value like NULL.

    Those functions must also use C calling convention so the caller cleans up the stack.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    The usual way is simply to pass along some sort of "end" value like NULL.
    Or pass a count value as the first parameter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    Or pass a count value as the first parameter.
    I'll admit, I never thought of that
    But then again, I don't use variable arguments very much.

    On a side note, the C++0x standard is supposed to fix this by introducing a new containers to hold variable arguments. Very nice, if I may say so.
    Of course, you can design your own such class. It wouldn't be very hard, I think.
    Last edited by Elysia; 01-25-2008 at 09:18 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeah, the more I think of it the uglier it is.

    For what I want a simple array or vector would do since it won't be a heavily user input based class. Since hte class I need it for has a vector member that they will assign to anyway I can do it pretty easily, or just take it input wise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 10
    Last Post: 09-27-2005, 12:49 PM