Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getting a pointer from a string

Posted on 2000-04-15
12
Medium Priority
?
180 Views
Last Modified: 2010-04-02
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++.
0
Comment
Question by:coolmac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2719501
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;
0
 

Author Comment

by:coolmac
ID: 2720149
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.
0
 

Author Comment

by:coolmac
ID: 2720150
Adjusted points from 50 to 100
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:coolmac
ID: 2720151
Increasing points to 100
0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 300 total points
ID: 2720164
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);
container[pickme].DoSomething();
0
 

Expert Comment

by:nimrodh
ID: 2720171
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.
0
 

Author Comment

by:coolmac
ID: 2720186
Will still have to change my program a bit, but this will help and probably make it better in the end.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2720274
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.
0
 

Author Comment

by:coolmac
ID: 2722318
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?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2724599
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();
0
 

Author Comment

by:coolmac
ID: 2725790
I'll try that. Thanks.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2725919
Let me know if you need any more help. Templates can be hell if you're not used to them.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question