471,078 Members | 831 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 2353
* 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Francisco | last post: by
4 posts views Thread by matevz bradac | last post: by
4 posts views Thread by Augustus S.F.X Van Dusen | last post: by
14 posts views Thread by Luiz Antonio Gomes Pican?o | last post: by
23 posts views Thread by Russ Chinoy | last post: by
3 posts views Thread by prasanthag | last post: by
8 posts views Thread by Fredrik Tolf | last post: by
3 posts views Thread by Seth Gecko | last post: by
reply views Thread by leo001 | last post: by

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.