Solved

getting a pointer from a string

Posted on 2000-04-15
12
169 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
  • 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now