hard C++ question!

Nessoldaccount

Senior member
Jun 4, 2000
483
0
0
Let's say you have a string containing "anandtech"

you have a variable named "anandtech"

how can I take the string and match it up with the variable of the same name to perform a function?
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
This doesn't quite make sense to me... you have a string "anandtech" and you want to match it up against another string "anandtech", and if it proves equal, you want to call a function?

Well, unless I'm way off base (in which case you'll need to clarify yourself), this would be a general solution:

char *s = "anandtech";
if (!strcmp(s, "anandtech"))
doSomething();

 

Nessoldaccount

Senior member
Jun 4, 2000
483
0
0
no it's like this.

string name = "anandtech"
variable anandtech

if(contents of name == name of variable)
doSomething();
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Pretender I think he wants to have the user input a string, and basicly it takes that input as the name of the function and runs it. Supposedly there are too many functions to use a switch or if statement.
 

MickySoft

Member
Oct 23, 2000
77
0
0
Well, do you know the variable name that you want to use? If you do, then you could just use a simple strcmp function. If not, then I believe it's very hard(if it's possible) to know the name of the variable during execution time.
 

prodigy

Lifer
Oct 9, 1999
14,822
1
0


<< Pretender I think he wants to have the user input a string..... >>



Heh, did you think that was Pretender responding because of the &quot;snowman&quot; icon?
 

hans007

Lifer
Feb 1, 2000
20,212
18
81
that seems very close to impossible. Why would you want a user to reference a specific variable name anyways, you cant have infinte variables and the user could inputanything. The inner code is supposed to be transparent to the user, maybe you make an object with a name in its private member, but i dont understand why you'd do it with just a plain variable.
 

Just use a hashtable to key the variable off of the string.

There is no way at runtime for the language to know the name of the variable, so this is going to be the cleanest way to do it.

int foo = 100;
int bar = 200;
...
map m = map<char* int>;
m.add(&quot;foo&quot;, foo);
m.add(&quot;bar&quot;, bar);
...
void doSomeThing(char* key)
{
____if(m.hasKey(key))
_______int i = (int)m.get(key);
...
}
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
or you can use the # preprocessor operator...lets say we define VARSTRCMP to return 1 if the contents of str match the name of var.


#define VARSTRCMP(var, str) !strcmp(&quot;&quot;#var&quot;&quot;, (const char*)str);

int main(int argc, void **argv)
{
int anandtechVar = 5;
const char anandtechStr[] = &quot;anandtechVar&quot;;

int compare = VARSTRCMP(anandtechVar, anandtechStr);

// compare is equal to 1 if the anandtechStr holds the value equal to the name of the first parameter - in this case anandtechStr. Otherwise it's 0 or 1.

return 0;
}


i think thats what you want.