G
Govindan
Michael B. Allen said:I need to create 1000's of objects that reference only a handful of strings.
Rather than allocate space for each redundant string I reasoned this string
member should just be a pointer to one of these handful of strings. The
question is; where to put the handful of strings? For that I decided a
static map and the below function would do the trick (pardon the MFC):
static CMap<CString,CString,CString,CString> strmap;
...
CString *CTest::GetStringPointer(CString& s)
{
if (strmap.Lookup(s, ?)) {
return ?;
}
strmap.SetAt(s, CString( s ));
return ?;
}
As you can see this is incomplete. For each string encountered I want to
look it up in the map. If it exists I return a pointer to it. If it does not
exist in the map I create a new string, add it to the map, and return a
pointer to it. I know passing the string by reference is a good idea and
returning the pointer is my objective but I cant use a CString on the stack
or the pointer returned will be invalid after the call completes.
So how do I get a *pointer* to the element in the map?
Thanks,
Mike
Use the STL , look at a hashmap implementation, use iterators rather than
pointers directly.