473,748 Members | 10,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(argume nts, num);

for(int i = 0; i < num; i++)
{
res += va_arg(argument s, 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(argume nts, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(argument s, 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 4340

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(argume nts, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(argument s, 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(argume nts, num);
int res = va_arg(argument s, int);

for(int i = 1; i < num; i++)
{
res -= va_arg(argument s, 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(argume nts, num);

for(int i = 0; i < num; i++)
{
res += va_arg(argument s, 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(argume nts, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(argument s, 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
2072
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 way here. I would appreciate any help. #try this to pass a list to a function and have the function return #a variable #this works list= def fctn(c):
110
9945
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 must be an object instead of
4
3408
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
7
2961
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 is address variable, which holds the memory address of the object. Using a pointer variable as a function parameter, the function has the ability to CHANGE the object value pointed by the pointer. Why needs pass by reference?
1
9476
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 something like %V to pass in another format string and a va_list to allow nesting. It happens to work on my compiler, but I wasn't sure if it's portable to use va_list's as parameters to a variable argument function because va_list isn't always just a...
10
13661
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 function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
6
2713
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 pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
11
3360
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 ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
12
3017
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.