<?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[How to cast QSharedDataPointer]]></title><description><![CDATA[<p dir="auto">Hi !</p>
<p dir="auto">How to  dynamic_cast a QSharedDataPointer&lt;Base&gt; object into a QSharedDataPointer&lt;Derived&gt; object whitout loosing the reference counting from the Base object ?</p>
<p dir="auto">NB: Base inherits QSharedData.<br />
NB: Derived inherits Base.</p>
]]></description><link>https://forum.qt.io/topic/28090/how-to-cast-qshareddatapointer</link><generator>RSS for Node</generator><lastBuildDate>Thu, 30 Apr 2026 19:51:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/28090.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 08 Jun 2013 16:45:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to cast QSharedDataPointer on Thu, 11 Jul 2013 05:57:11 GMT]]></title><description><![CDATA[<p dir="auto">I've thought about this a bit and think I may have a solution. The idea is that only the base class contains a QSharedDataPointer and the derived classes access it via static_cast.  It's essentially a form of static polymorphism.  Here's the code:</p>
<p dir="auto">@<br />
class BaseDataType<br />
{<br />
const char* getter() const;<br />
void  setter(const char*);<br />
};</p>
<p dir="auto">class DerivedDataType<br />
{<br />
const char* getter_derived() const;<br />
void  setter_derived(const char*);<br />
};</p>
<p dir="auto">class BaseData : public QSharedData<br />
{<br />
public:<br />
// the standard ctor and copy ctor go here</p>
<p dir="auto">// my data is a pointer to a polymorphic type<br />
BaseDataType* ptr;<br />
};</p>
<p dir="auto">class Base<br />
{<br />
public:<br />
// the standard ctor and copy ctor go here</p>
<p dir="auto">protected:<br />
// subclasses use this to access the shared ptr and then static cast it<br />
QSharedDataPointer&lt;BaseData&gt; real_d;<br />
};</p>
<p dir="auto">class Derived<br />
{<br />
public:<br />
// ctor and copy ctor go here</p>
<p dir="auto">const char* getter_derived() const<br />
{<br />
// const access - won't cause a detach<br />
return d()-&gt;getter_derived();<br />
}</p>
<p dir="auto">void setter_derived(const char* str)<br />
{<br />
// non-const access - may cause a detach<br />
d()-&gt;setter_derived(str);<br />
}</p>
<p dir="auto">private:<br />
// these accessors replace the standard "d" pointer:</p>
<p dir="auto">// the -&gt; won't cause a detach here<br />
const DerivedDataType* d() const {return static_cast&lt;DerivedDataType*&gt;(real_d-&gt;ptr);</p>
<p dir="auto">// the -&gt; may cause a detach here<br />
DerivedDataType* d() {return static_cast&lt;DerivedDataType*&gt;(real_d-&gt;ptr);}</p>
<p dir="auto">};<br />
@</p>
<p dir="auto">This only just occured to me, so somebody please let me know if I'm missing something.</p>
]]></description><link>https://forum.qt.io/post/185962</link><guid isPermaLink="true">https://forum.qt.io/post/185962</guid><dc:creator><![CDATA[wibstr]]></dc:creator><pubDate>Thu, 11 Jul 2013 05:57:11 GMT</pubDate></item><item><title><![CDATA[Reply to How to cast QSharedDataPointer on Wed, 10 Jul 2013 20:07:55 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="bjanuario" date="1370715462"]Can u have a look at "QSharedDataPointer sample":<a href="http://cdumez.blogspot.pt/2011/03/implicit-explicit-data-sharing-with-qt.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://cdumez.blogspot.pt/2011/03/implicit-explicit-data-sharing-with-qt.html</a></p>
<p dir="auto">Hope this helps ;)[/quote]</p>
<p dir="auto">It doesn't. There's nothing in that article about polymorphism or dynamic casting.</p>
<p dir="auto">I'm also interested in this question.  Does anyone have a good solution?</p>
]]></description><link>https://forum.qt.io/post/185927</link><guid isPermaLink="true">https://forum.qt.io/post/185927</guid><dc:creator><![CDATA[wibstr]]></dc:creator><pubDate>Wed, 10 Jul 2013 20:07:55 GMT</pubDate></item><item><title><![CDATA[Reply to How to cast QSharedDataPointer on Sat, 08 Jun 2013 18:17:42 GMT]]></title><description><![CDATA[<p dir="auto">Can u have a look at "QSharedDataPointer sample":<a href="http://cdumez.blogspot.pt/2011/03/implicit-explicit-data-sharing-with-qt.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://cdumez.blogspot.pt/2011/03/implicit-explicit-data-sharing-with-qt.html</a></p>
<p dir="auto">Hope this helps ;)</p>
]]></description><link>https://forum.qt.io/post/182129</link><guid isPermaLink="true">https://forum.qt.io/post/182129</guid><dc:creator><![CDATA[bjanuario]]></dc:creator><pubDate>Sat, 08 Jun 2013 18:17:42 GMT</pubDate></item></channel></rss>