Click to See Complete Forum and Search --> : What is Function with variable number of arguments?


Debkumar Chakraverty
05-07-2002, 10:20 AM
What is Function with variable number of arguments?
Can u apply Functions with variable no. of arguments with objects?

Danny Kalev
05-07-2002, 11:10 AM
Debkumar Chakraverty wrote:
>
> What is Function with variable number of arguments?
> Can u apply Functions with variable no. of arguments with objects?

No, you can't do that with function objects because they take a hidden
argument (the this pointer). See
http://gethelp.devx.com/techtips/cpp_pro/10min/2001/feb/10min0201.asp

Danny

ralph
05-07-2002, 12:47 PM
"Debkumar Chakraverty" <debkumar_c@rediffmail.com> wrote:
>
>What is Function with variable number of arguments?
>Can u apply Functions with variable no. of arguments with objects?


"What is Function with variable number of arguments?"

One that can take any number of arguments at runtime. <smile> An example
of such a function is printf(). They are unique to C/C++ (Other languages
provide a similar mechanism using a 'variant' type of array). In fact the
default calling convention in C was partially developed to support variable
argument lists. The calling convention (_cdecl) passes arguments in right
to left order on the stack and the calling function becomes responsible for
cleaning up the stack. The first part of this is what allows variable args
functions to work.

"Can u apply Functions with variable no. of arguments with objects?"

Yes you can - kind of...

[I'll try and make this brief - not easy for me. <g>]
First in order to use them you have to use the macros included in the <cstdarg>
header (Some Unixs use a similar lib - varargs.h) These macros in a sense
'capture' the point on the stack where the arguments are loaded and then
marks the 'end' of the list. You then can remove them in order.

int foo( int First, ... )
{
va_list V; // marks the start of the stack
va_start( V, First ); // First is the end of the stack
...
int iJunk;
char cJunk;
va_arg( V, iJunk ); // will pull an int
va_arg( V, cJunk ); // will pull the next sequence of bytes to fill
// a character *** A Gotcha! ****
// the compiler will promote any 'chars'
// passed in to an int
// it will also promote any floats to
// doubles, etc. So you have to be careful.
va_end(V);
}

Therefore you could use this mechanism for "objects" if they are passed
by value (reference/pointer) and you were very careful in pulling the address
off the stack and properly "casting" them to the necessary types. (A union
is useful for handling this.)

In general tho I believe you will find that this technique is less desireable
than it may appear at first . It works for passing items of various integal
types, but for different objects you have to already "know" their position
in the stack in order to properly "cast" them. Or invoke some other exotic
RTTL scheme - same base class, same outside wrapper, etc. And you can only
have one per class.

For a variable list of the same object a vector would work as well and likely
be more reliable.

-ralph