Calling a fn thru String???

Actually, i just want to call a function like this
mov(a,b);
But, the situation I am facing is I have "mov","a","b" as three string variables.

Can I call the function using these three variables i want to call mov(a,b)
Here,a and b are int datatypes
Please help me I need to know that can I do it or not. Other wise, I hav to start writing if statements to identify the function name from the list of functions I hav and even with parameters...........
I came across with this situation while im tryng to do my project in c++.
Thank u................
Last edited on
It can't be done directly. C++ has no reflection.
You can approximate the behavior, though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <map>

typedef int (*function_t)(int,int);

int add(int a,int b){
	return a+b;
}

int multiply(int a,int b){
	return a*b;
}

int main(){
	std::map<std::string,function_t> functions;
	functions["add"]=add;
	functions["multiply"]=multiply;

	std::cout <<functions["add"](1,2)<<std::endl
		<<functions["multiply"](1,2)<<std::endl;
}
This interests me as well.

If I have a list of a million labels and I want to output the value of one that has exceeded a limit, would I need to use reflection to also output the name of that label? Which language would be best for something like this?
This is really interesting .
If I have a list of a million labels and I want to output the value of one that has exceeded a limit, would I need to use reflection to also output the name of that label?
It all depends on how you're storing them. Reflection is used to access at run time, information that would otherwise only be available at compile time, such as variable names. So, if your labels are stored, for example, as
1
2
3
label0="foo";
label1="bar";
//... 
then you're a very silly person and you do need reflection.
If, however, they're stored as
1
2
3
labels[0]="foo";
labels[1]="bar";
//... 
then you don't.
In my case my labels are stored as a massive shared database, shared with a .h file between several DLLs.

It starts off with a pointer to a memory address and then offsets each variable from that label.
1
2
3
4
5
6
7
extern int &LabelA1 = SHARED_BASE+0;
extern int &LabelA2 = SHARED_BASE+4;
extern int &LabelA3 = SHARED_BASE+8;
extern char &LabelA4 = SHARED_BASE+12;
extern double &LabelA5 = SHARED_BASE+13;
extern int &LabelA6 = SHARED_BASE+19;
....


Then I can use LabelA1, LabelA2, LabelA3 etc... on any of my networked machines provided that I send updates between my machines and that I have the same DLL/Lib on each. However, I'd like to be able to debug these labels by name.
Last edited on
And they're heterogeneous to boot? Who was the madman who designed this?
thanx helios......
thanx a lot!!!!!
Topic archived. No new replies allowed.