Solved

getting a pointer from a string

Posted on 2000-04-15
12
176 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
KangaRoo earned 100 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

707 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