473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to pass variable arg list to a function pointer

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 that function (using its
pointer) and call it with the same argument list that was passed into
the static method.

sorry the bad explanation, maybe the code will make it clearer:

#include <iostream>
#include <stdarg.h>

//just a function to sum a variable number of integers
int Sum(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res += va_arg(arguments, int);
}
return res;
}

//just a function to sub a variable number of integers
int Sub(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}

class Operation
{
public:
static int Calc(int (*op)(int num, ...), int num, ...)
{
//here is where the problem exists, how can i pass that argument
list to the op function?
return (*op)(num, ...);//ofc this gives me a syntax error.
}
};

int main(int argc, char *argv[])
{
//usage example
std::cout << Operation::Calc(Sum, 3, 1, 2, 3) << std::endl;//should
print 6 (1+2+3)
return 0;
}

thanks in advance :)

Aug 1 '06 #1
3 4315

ca*************@gmail.com wrote:
//just a function to sub a variable number of integers
int Sub(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}
just a small EDIT, 'cause the function wasnt working like it should be,
its not really important to the problem it self, but ok.
correct way:

int Sub(int num, ...)
{
va_list arguments;
va_start(arguments, num);
int res = va_arg(arguments, int);

for(int i = 1; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}

Aug 1 '06 #2
ca*************@gmail.com wrote:
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 that function (using its
pointer) and call it with the same argument list that was passed into
the static method.

[..]
There is no way. You need to implement your 'Sum' and 'Sub' to take
'va_list' instead.

Find an implementation of "fprintf" + "vfprintf". The former takes the
ellipsis argument, the latter takes a 'va_list'. The former calls the
latter. You need to follow that pattern.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #3
ca*************@gmail.com wrote:
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 that function (using its
pointer) and call it with the same argument list that was passed into
the static method.

sorry the bad explanation, maybe the code will make it clearer:

#include <iostream>
#include <stdarg.h>

//just a function to sum a variable number of integers
int Sum(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res += va_arg(arguments, int);
}
return res;
}

//just a function to sub a variable number of integers
int Sub(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}

class Operation
{
public:
static int Calc(int (*op)(int num, ...), int num, ...)
{
//here is where the problem exists, how can i pass that argument
list to the op function?
return (*op)(num, ...);//ofc this gives me a syntax error.
}
};

int main(int argc, char *argv[])
{
//usage example
std::cout << Operation::Calc(Sum, 3, 1, 2, 3) << std::endl;//should
print 6 (1+2+3)
return 0;
}

thanks in advance :)

Mmmhhh ... I remember reading somewhere in this list, not a long time
ago that it was not possible with the current C++ standard ...

I think the common work-around is to use a macro that defines the
function with different number of parameters. You can have a look at
Boost.Function (http://www.boost.org/doc/html/function.html). But the
idea is to have in the end something like that:

template <class R>
R calc( R(*op)() )
{
return op();
}

template <class R, class Arg1>
R calc( R (*op)(Arg1), Arg1 value )
{
return op(value);
}

template <class R, class Arg1, class Arg2>
R calc( R (*op)(Arg1,Arg2), Arg1 value1, Arg2 value2)
{
return op(value1, value2);
}

....
But IRC, it is a bit more complex if you want to handle reference types
(in that case, better use Boost.Function !!)

Pierre
Aug 1 '06 #4

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

Similar topics

10
by: Doug Jordan | last post by:
I am fairly new to Python. This should be an easy answer but I cannot get this to work. The code is listed below. I know how to do this in C, Fortran, and VB but it doesn't seem to work the same...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
7
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.