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++.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

KangaRooConnect With a Mentor Commented:
So the problem is: given a set of objects, how to randomly select one object of that set?

Depends on the container you use to store the objects off course, but a straightforward way could be:

class SomeClass; // the class of objects we are using
// ....

// random pick
vector<SomeClass> container; // contains the objects (F1, F2, ....)
int pickme = rand() * container.size() / (RAND_MAX + 1);
It would help if you just explained what you want. The code snippet doesn't help.

Two concatenate two strings use something like


using namespace std;

string str1("Hello");
string str2(" world");
string sum = str1 + str2;
coolmacAuthor Commented:
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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

coolmacAuthor Commented:
Adjusted points from 50 to 100
coolmacAuthor Commented:
Increasing points to 100
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.
coolmacAuthor Commented:
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.
coolmacAuthor Commented:
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();
coolmacAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.