<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Can you have a pointer to a QHash (or other implicitly shared types)?]]></title><description><![CDATA[<p dir="auto"><em>Issue:</em><br />
I have two (2) QHashes in a function. I want to use a pointer to only one of them when the function is called. This block of code conceptually sums up what I'd like to do:<br />
@<br />
QHash&lt;int, int&gt; *hashptr;<br />
QHash&lt;int, int&gt; firstQHash;<br />
QHash&lt;int, int&gt; secondQHash;<br />
hashptr = (goodOrBad) ? &amp;firstQHash : &amp;secondQHash;<br />
do {<br />
lots of stuff;<br />
with whatever hash;<br />
I happen to be pointing at;<br />
} while(thingsAreGood);<br />
@</p>
<p dir="auto">The problem is it doesn't work. hashptr seems to only have a subset of functionality availible to it (e.g. it compiles with "contains(key)", but I can't assign a value to it (hashptr[key] = value) . There are other ways to do what I'm after, but I'd like to understand why it doesn't work (what I'm doing wrong) so I don't keep doing the same kind of mistake.</p>
]]></description><link>https://forum.qt.io/topic/49875/can-you-have-a-pointer-to-a-qhash-or-other-implicitly-shared-types</link><generator>RSS for Node</generator><lastBuildDate>Sun, 15 Nov 2026 21:16:00 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/49875.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 07 Jan 2015 16:37:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Can you have a pointer to a QHash (or other implicitly shared types)? on Wed, 07 Jan 2015 17:20:27 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="wolf." date="1420650531"]Thank you for being so kind. I realized after posting that the problem was I wasn't dereferencing, but I'm glad I posted anyway. I wasn't familiar with using references, so I haven't been using them. It's way cleaner.</p>
<p dir="auto">[/quote]</p>
<p dir="auto">If you are unfamiliar with references, than I strongly suggest that you look at "this FAQ":<a href="https://isocpp.org/wiki/faq/references" target="_blank" rel="noopener noreferrer nofollow ugc">https://isocpp.org/wiki/faq/references</a>.</p>
<p dir="auto">When you have some confidence with them, you could continue with "const correctness":<a href="https://isocpp.org/wiki/faq/const-correctness" target="_blank" rel="noopener noreferrer nofollow ugc">https://isocpp.org/wiki/faq/const-correctness</a>.</p>
]]></description><link>https://forum.qt.io/post/256551</link><guid isPermaLink="true">https://forum.qt.io/post/256551</guid><dc:creator><![CDATA[ckakman]]></dc:creator><pubDate>Wed, 07 Jan 2015 17:20:27 GMT</pubDate></item><item><title><![CDATA[Reply to Can you have a pointer to a QHash (or other implicitly shared types)? on Wed, 07 Jan 2015 17:08:51 GMT]]></title><description><![CDATA[<p dir="auto">Thank you for being so kind. I realized after posting that the problem was I wasn't dereferencing, but I'm glad I posted anyway. I wasn't familiar with using references, so I haven't been using them. It's way cleaner.</p>
]]></description><link>https://forum.qt.io/post/256550</link><guid isPermaLink="true">https://forum.qt.io/post/256550</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 07 Jan 2015 17:08:51 GMT</pubDate></item><item><title><![CDATA[Reply to Can you have a pointer to a QHash (or other implicitly shared types)? on Wed, 07 Jan 2015 16:57:04 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">The problem you have is not a QHash but a C++ problem. In your example code, you have pointer to QHash, therefore when you use QHash, you need to dereference the pointer. Here are some examples:<br />
@<br />
(*hashptr)[key] = value;<br />
hashptr-&gt;insert(key, value);<br />
hashptr-&gt;contains(key);<br />
(*hashptr).contains(key);<br />
@</p>
<p dir="auto">However you can do better if you use a reference:<br />
@<br />
QHash&lt;int, int&gt; &amp;hash = goodOrBad ? firstQHash : secondQHash;</p>
<p dir="auto">hash[key] = value;<br />
hash.contains(key);<br />
...<br />
@</p>
]]></description><link>https://forum.qt.io/post/256548</link><guid isPermaLink="true">https://forum.qt.io/post/256548</guid><dc:creator><![CDATA[ckakman]]></dc:creator><pubDate>Wed, 07 Jan 2015 16:57:04 GMT</pubDate></item></channel></rss>