473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pointers from References

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
Jul 19 '05 #1
6 3709
Michael B. Allen wrote:
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;


Your use of CMap and CString is offtopic here, better ask this in a
Microsoft NG.

mfg

Christoph

Jul 19 '05 #2
"Michael B. Allen" <mb*****@ioplex.com> wrote in message
news:bj**********@bob.news.rcn.net...
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;
Neither CMap nor CString are part of standard C++. You are more likely
to get a good peer reviewed answer if you post this question to a
newsgroup with mfc in its name, such as
news://comp.os.ms-windows.programmer.tools.mfc and
news://microsoft.public.vc.mfc . See also:
http://www.slack.net/~shiva/welcome.txt and
http://home.wanadoo.nl/efx/c++-faq/.
...
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.


Looking at this description I think you are better of replacing CMap
with std::map, which is not only standard, but also implements the
behaviour you are looking for. If the code is not strictly UI related,
you might also want to consider replacing CString with std::string. Use
standard classes when you (reasonably) can, use non-standard classes
when you have to.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #3

"Michael B. Allen" <mb*****@ioplex.com> wrote in message
news:bj**********@bob.news.rcn.net...
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.
Jul 19 '05 #4

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
Michael B. Allen wrote:
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;


Your use of CMap and CString is offtopic here, better ask this in a
Microsoft NG.

mfg

Christoph


Geez, folks...just because someone uses classes that are defined in MFC,
that doesn't make the question off-topic. What if those were simply
user-defined classes, like MyMap and MyString? Would that be off-topic?
Sure, you may need more info on the behavior of MyMap (or CMap in this
case), but it's still a valid C++ language question.

The OP's problem is how to return a reference or pointer to a member of a
collection, possibly while adding that as a new member first. A perfectly
legitimate question, and other responders have provided helpful info, such
as using iterators instead.

Let's not be so quick to jump on anyone who uses a class that just happens
to be written by Microsoft, ok? They're not *totally* evil! :-)

-Howard

Jul 19 '05 #5

"Ivan Vecerina" <iv**@myrealbox.com> wrote in message
news:3f******@news.swissonline.ch...
The behavior you want would easily be implemented with
the std::set collection, rather than a map:
Yes. I see.

static std::set<CString> strset;

CString& CTest::GetStringPointer(CString& s)
{
return & * strset.insert(s).first;
}

Or better IMHO:

CString& CTest::GetStringReference(CString& s)
{
return * strset.insert(s).first;
}


Mmm, I take it one of these returns a reference to a pointer and the other
returns a reference to the object in the map? Is the & in the return
statement in the first example the address-of operator or is that some way
to get a reference to something? If the later is true why is it necessary
for return values when it isn't necessary for parameters?

Is this the method being used?

pair<iterator, bool> insert(const value_type& x)

How is 'iterator' defined?

I just realized, here I go again, I'm learning a new language! Do you think
I can put this on my resume yet ;->

Mike


Jul 19 '05 #6
Hi Michael,
"Michael B. Allen" <mb*****@ioplex.com> wrote in message
news:bj**********@bob.news.rcn.net...
"Ivan Vecerina" <iv**@myrealbox.com> wrote in message
news:3f******@news.swissonline.ch... ....
static std::set<CString> strset;
Sorry, typo here: CString& CTest::GetStringPointer(CString& s) What I meant was:
CString* CTest::GetStringPointer(CString& s) {
return & * strset.insert(s).first;
}

Or better IMHO:

CString& CTest::GetStringReference(CString& s)
{
return * strset.insert(s).first;
}


Mmm, I take it one of these returns a reference to a pointer and the other
returns a reference to the object in the map? Is the & in the return
statement in the first example the address-of operator or is that some way
to get a reference to something? If the later is true why is it necessary
for return values when it isn't necessary for parameters?

It was a plain address-of operator, to obtain a pointer from a reference.
Is this the method being used?

pair<iterator, bool> insert(const value_type& x) Yes, exactly.
How is 'iterator' defined? Iterators are objects that behave very much like pointers: they refer to
an object within a collection (rather than a contiguous array). They
support a similar set of operations ( * and -> to access the item,
++ or -- to move to the next/previous item, etc ).
Each C++ collection defines an iterator (and a const_iterator), providing
a way to traverse its items.

BTW, the function could return an iterator instead of a pointer or
reference -- but this would expose an implementation detail (that is,
users of the function would have to specifically use
std::set<CString>::iterator instead of CString& or CString*.
And because users don't need to see the previous or next item,
there is no benefit in returning an iterator.
This is why the functions dereference the iterator to obtain
a reference (or the address of) the item itself.

They reason why I would return a reference (CString&) instead of
a pointer (CString*) is that the reference indicates that a
valid object is always returned -- while the pointer suggests
that NULL could be returned. This is a style choice.
I just realized, here I go again, I'm learning a new language!
Do you think I can put this on my resume yet ;->

Well, just another library ... but a standard one, which BTW
can be quite powerful.
Cheers,
Ivan
--
http://www.post1.com/~ivec <> Ivan Vecerina
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Web Developer | last post by:
Hi, Can someone provide a short and concise statement(s) on the difference between pointers and references. A graphical representation (via links?) of both would be much appreciated as well. ...
4
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers...
26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
12
by: arganx | last post by:
Before I had a good idea how to use pointers, I was wondering - first, how to use them. And second, of what value would they be. Now that I have at least a pretty good idea of how to use them, I...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
7
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
29
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.