Link to home
Start Free TrialLog in
Avatar of coolmac
coolmac

asked on

getting a pointer from a string

I use the following script as part of a mouse over button java script to use a string (imgName) to help define what picture you are using.

imgdown=eval(imgName + "down.src");
document [imgName].src=imgdown;

I was wondering if there is any way that this can be done in C++.
Avatar of KangaRoo
KangaRoo

It would help if you just explained what you want. The code snippet doesn't help.

Two concatenate two strings use something like

#include<string>

using namespace std;

string str1("Hello");
string str2(" world");
string sum = str1 + str2;
Avatar of coolmac

ASKER

Sorry about that, the problem is so clear in the mind that you forget others don't know what you are getting at. :)

How do I get two concatenated strings to then point to an object or variable.
I originally wanted to do this with a program that genrated random numbers and then modified one of many fields on a form based on that number. Now I ended up using an array and changeing the entire way I went around codeing the form. But I was just wondering if I have the fields numbered F1, F2, F3 etc.
If I make a string out of the generated number and append it to and "F", is there any way that I can use that string to now point to the field that it is supposed to without codeing a huge case statement?
That was what the java script at the top was refering to. It allowed me to reference whatever image was current with the same line of code instead of going if image = 1 show img1
if image = 2 show img2 etc.
Avatar of coolmac

ASKER

Adjusted points from 50 to 100
Avatar of coolmac

ASKER

Increasing points to 100
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
why not use a hash table with the field name as key and a pointer as the value? I realize this is kind of like coding a switch statement but it more dynamic and managable.
Avatar of coolmac

ASKER

Will still have to change my program a bit, but this will help and probably make it better in the end.
You could use a hash table or std::set as well. The vector was just an example and the indexing is more natural with random numbers.
Avatar of coolmac

ASKER

I would still like to know whether the string thing could be done. This is just out of interest and I haven't actually needed it yet. But say you have an object (call it fldname)
and now you have a string  with "fldname" as its value,
is there anyway to use that string to get a pointer to point to the object?
Or is that just not possible in C?
You could associate those objects with their names in a map:
map<SomeClass, string> container;
Store the SomeClass objects together with the identifying names

MyClass anObject;
string aName("HiThere")
container.insert(pair<string, MyClass>(aName, anObject));

container::iterator iter = container.find(aName);
if(iter != container.end())  iter->DoSomething();
Avatar of coolmac

ASKER

I'll try that. Thanks.
Let me know if you need any more help. Templates can be hell if you're not used to them.