473,516 Members | 3,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing variable arguments for future use in C++?

Hi,
I am a newbie to this group. I have a problem in handling the variable

arguments passed to a function. My requirement is like this.

I have 2 functions say,
void funcX(int i, int j);
void funcY(int i, int j,char *name);
Now I want to register this function some how and store the variable
arguments ( and store defualt values) for future use
RegisterFunc(funcX,10,20);
RegisterFunc(funcY,50,60,"MyName");
( Here I want to know how I can register and store the variable
arguments and function pointers, so that it can be used at a later
point to invoke the same)
Now at a later point I want to evoke these store functions in a
sequential order.
CallFuncs()
{
//Invoke funcX with values registered here;
//Invoke funcY with values registered here;

}
I was trying to use va_args with C and somehow i was not able to be
successful. If anybody have a better suggestion/design on how to do it,

please let me know.

May 29 '06 #1
2 2475
* pr********@gmail.com:
Hi,
I am a newbie to this group. I have a problem in handling the variable

arguments passed to a function. My requirement is like this.

I have 2 functions say,
void funcX(int i, int j);
void funcY(int i, int j,char *name);
Now I want to register this function some how and store the variable
arguments ( and store defualt values) for future use
RegisterFunc(funcX,10,20);
RegisterFunc(funcY,50,60,"MyName");
Check out the boost facilities for functor objects.

Store them in a std::vector.

( Here I want to know how I can register and store the variable
arguments and function pointers, so that it can be used at a later
point to invoke the same)
Now at a later point I want to evoke these store functions in a
sequential order.
CallFuncs()
{
//Invoke funcX with values registered here;
//Invoke funcY with values registered here;
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 29 '06 #2
pr********@gmail.com wrote:
Hi,
I am a newbie to this group. I have a problem in handling the variable

arguments passed to a function. My requirement is like this.

I have 2 functions say,
void funcX(int i, int j);
void funcY(int i, int j,char *name);
Now I want to register this function some how and store the variable
arguments ( and store defualt values) for future use
RegisterFunc(funcX,10,20);
RegisterFunc(funcY,50,60,"MyName");
( Here I want to know how I can register and store the variable
arguments and function pointers, so that it can be used at a later
point to invoke the same)
Now at a later point I want to evoke these store functions in a
sequential order.
CallFuncs()
{
//Invoke funcX with values registered here;
//Invoke funcY with values registered here;

}
I was trying to use va_args with C and somehow i was not able to be
successful.
Those cannot be used for what you want.
If anybody have a better suggestion/design on how to do it,
please let me know.


You could use function objects.

#include <iostream>
#include <vector>

class Func
{
public:
virtual void operator()() = 0;
};

class FuncX : public Func
{
public:
FuncX(int i, int j)
: i_(i), j_(j)
{}
void operator()()
{
std::cout << i_ << '+' << j_ << '=' << i_+j_ << '\n';
}
private:
int i_, j_;
};

class FuncY: public Func
{
public:
FuncY(int i, int j, const char* name)
: i_(i), j_(j), name_(name)
{
}
void operator()()
{
std::cout << name_ << '=' << i_ << '+' << j_ << '\n';
}
private:
int i_, j_;
const char* name_;
};

std::vector<Func*> funcs;

void RegisterFunc(Func* func)
{
funcs.push_back(func);
};

void CallFuncs()
{
for (std::vector<Func*>::iterator it = funcs.begin(), end = funcs.end();
it != end; ++it)
{
(**it)();
}
}

void CleanUp()
{
for (std::vector<Func*>::iterator it = funcs.begin(), end = funcs.end();
it != end; ++it)
{
delete *it;
}
funcs.clear();
}

int main()
{
RegisterFunc(new FuncX(10, 20));
RegisterFunc(new FuncY(50, 60, "MyName"));
CallFuncs();
CleanUp();
}

May 29 '06 #3

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

Similar topics

2
1756
by: Francisco | last post by:
I have this problem: I have a database with information about games, and users are able to vote for them. Everytime a user votes for a game I store the unique game name into a session variable (an array). So if they are in a page were they already voted, they won't have the option to do so. The idea is that the session cookie lasts "forever",...
4
1931
by: matevz bradac | last post by:
Hi, I'm trying to implement delayed function execution, similar to OpenGL's display lists. It looks something like this: beginList (); fcn1 (); fcn2 ();
4
13556
by: Augustus S.F.X Van Dusen | last post by:
I have recently come across the following construction: #define P_VAR(output, string, args...) \ fprintf (output, "This is "string"\n", ##args) which can be invoked as follows: int x = 1, y = 2 ; char * str = "String" ;
4
2927
by: Daniel | last post by:
I need to build the maze board(like 2d array) using a tree. But I find that my buildTree function doesn't work. Could anyone give me some hints on debugging it? Thanks bool BuildTree(TreeNodePtr *T, int x, int y) { if (boardSize == 0) //boardSize is a global variable { return TRUE; } else {
14
5809
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I want to store data like a database: ---------------------------------- Custumer:
23
19134
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
2500
by: prasanthag | last post by:
Hi, I am a newbie to this group. I have a problem in handling the variable arguments passed to a function. My requirement is like this. I have 2 functions say, void funcX(int i, int j); void funcY(int i, int j,char *name);
8
1990
by: Fredrik Tolf | last post by:
I've been trying to get the string formatting operator (%) to work with more arguments than the format string requires, but I can find no way to do that. For example: '10' Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: not all arguments converted during string formatting The thing is, I want to get format...
3
2248
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) The parent form have a custom control, which have a public sub looking
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7405
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. ...
0
7547
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...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4769
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...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.