<?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[Good way of converting user-defined pointers]]></title><description><![CDATA[<p>Let us imagine that I have a class which has pointer semantics:</p>
<pre><code class="language-cpp">template&lt;class T&gt; class MyPointer
{ //This class differs from simple pointer by having someAdditionalData
private:
  T *pointer;
  int someAdditionalData;
public:
  inline MyPointer(void): pointer(NULL), someAdditionalData(0) {;} //Default constructor
  inline MyPointer(MyPointer&lt;T&gt; const &amp;p): pointer(p.pointer), someAdditionalData(p.someAdditionalData) {;} //Copy constructor
  inline MyPointer(T *const p): pointer(p), someAdditionalData(0) {;} //Custom constructor
  inline MyPointer&lt;T&gt; *operator-&gt;(void) const {return pointer;} //Arrow operator
  inline MyPointer&lt;T&gt; &amp;operator*(void) const {return *pointer;} //Dereference operator
  inline int &amp;SomeAdditionalData(void) {return someAdditionalData;} //Really important method
};
</code></pre>
<p>I also have another class which this pointer points to:</p>
<pre><code class="language-cpp">struct ClassToPointTo
{
  int firstMember;
  int secondMember;
};

MyPointer&lt;ClassToPointTo&gt; GeneratePointer(void)
{
  //Some code here which calculates pointer and its someAdditionalData
}

...

MyPointer&lt;ClassToPointTo const&gt; const p( GeneratePointer() ); //I really like to write &quot;const&quot; where I can
</code></pre>
<p>The first problem is that <code>MyPointer&lt;ClassToPointTo&gt;</code> is not automatically converted into <code>MyPointer&lt;ClassToPointTo const&gt;</code> (I know why, but I don't know how to fix it), and the second, the main problem is following:</p>
<p>I need to convert pointer <code>p</code> from &quot; <code>ClassToPointTo const</code> &quot; type into one that will point to the <code>firstMember</code> of <code>*p</code> object *without losing <code>someAdditionalData</code> *. I have function <code>GeneratePointer()</code> which will make pointer having <code>someAdditionalData</code> , but I need to work only with pointers to <code>firstMember</code> s of the objects:</p>
<pre><code class="language-cpp">MyPointer&lt;int const&gt; const p( WhatINeedToWriteHere??? GeneratePointer() ??? );
</code></pre>
<p>I need fast and good-looking way of doing such conversion.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/261103/good-way-of-converting-user-defined-pointers</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 17:03:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/261103.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 13 Feb 2010 20:18:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Sat, 13 Feb 2010 20:18:05 GMT]]></title><description><![CDATA[<p>Let us imagine that I have a class which has pointer semantics:</p>
<pre><code class="language-cpp">template&lt;class T&gt; class MyPointer
{ //This class differs from simple pointer by having someAdditionalData
private:
  T *pointer;
  int someAdditionalData;
public:
  inline MyPointer(void): pointer(NULL), someAdditionalData(0) {;} //Default constructor
  inline MyPointer(MyPointer&lt;T&gt; const &amp;p): pointer(p.pointer), someAdditionalData(p.someAdditionalData) {;} //Copy constructor
  inline MyPointer(T *const p): pointer(p), someAdditionalData(0) {;} //Custom constructor
  inline MyPointer&lt;T&gt; *operator-&gt;(void) const {return pointer;} //Arrow operator
  inline MyPointer&lt;T&gt; &amp;operator*(void) const {return *pointer;} //Dereference operator
  inline int &amp;SomeAdditionalData(void) {return someAdditionalData;} //Really important method
};
</code></pre>
<p>I also have another class which this pointer points to:</p>
<pre><code class="language-cpp">struct ClassToPointTo
{
  int firstMember;
  int secondMember;
};

MyPointer&lt;ClassToPointTo&gt; GeneratePointer(void)
{
  //Some code here which calculates pointer and its someAdditionalData
}

...

MyPointer&lt;ClassToPointTo const&gt; const p( GeneratePointer() ); //I really like to write &quot;const&quot; where I can
</code></pre>
<p>The first problem is that <code>MyPointer&lt;ClassToPointTo&gt;</code> is not automatically converted into <code>MyPointer&lt;ClassToPointTo const&gt;</code> (I know why, but I don't know how to fix it), and the second, the main problem is following:</p>
<p>I need to convert pointer <code>p</code> from &quot; <code>ClassToPointTo const</code> &quot; type into one that will point to the <code>firstMember</code> of <code>*p</code> object *without losing <code>someAdditionalData</code> *. I have function <code>GeneratePointer()</code> which will make pointer having <code>someAdditionalData</code> , but I need to work only with pointers to <code>firstMember</code> s of the objects:</p>
<pre><code class="language-cpp">MyPointer&lt;int const&gt; const p( WhatINeedToWriteHere??? GeneratePointer() ??? );
</code></pre>
<p>I need fast and good-looking way of doing such conversion.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855176</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855176</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 13 Feb 2010 20:18:05 GMT</pubDate></item><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Sat, 13 Feb 2010 20:53:12 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>The first problem is that <code>MyPointer&lt;ClassToPointTo&gt;</code> is not automatically converted into <code>MyPointer&lt;ClassToPointTo const&gt;</code> (I know why, but I don't know how to fix it)</p>
</blockquote>
<p>Maybe like this?</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
class MyPointer;

template &lt;typename T&gt;
struct MyPointerConversion
{
    operator MyPointer&lt;const T&gt;() const
    {
        // ...
    }
};

template &lt;typename T&gt;
struct MyPointerConversion&lt;const T&gt;
{
};

template &lt;typename T&gt;
class MyPointer : public MyPointerConversion&lt;T&gt;
{
    // ...
};
</code></pre>
<p>A conversion constructor doesn't work because you would have to write</p>
<pre><code class="language-cpp">MyPointer(const MyPointer&lt;typename remove_const&lt;T&gt;::type&gt;&amp; origin);
</code></pre>
<p>which might have the same signature as the copy constructor ( <code>remove_const</code> is a metafunction that removes the <code>const</code> qualifier from a type).</p>
<p>Concerning your second problem: Can't it be solved by using a conversion operator (or better a function, to avoid implicit conversions), too?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855186</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855186</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 13 Feb 2010 20:53:12 GMT</pubDate></item><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Sat, 13 Feb 2010 21:35:09 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>A conversion constructor doesn't work because you would have to write</p>
<pre><code class="language-cpp">MyPointer(const MyPointer&lt;typename remove_const&lt;T&gt;::type&gt;&amp; origin);
</code></pre>
<p>which might have the same signature as the copy constructor ( <code>remove_const</code> is a metafunction that removes the <code>const</code> qualifier from a type).</p>
</blockquote>
<p>sfinae helps</p>
<pre><code class="language-cpp">template &lt;typename U&gt;
MyPointer(const MyPointer&lt;U&gt;&amp; origin, typename enable_if&lt; is_same&lt; T, const U &gt; &gt;::type* = 0);
</code></pre>
<p>A template constructor is never a copy constructor.<br />
with meta functions is_same and enable_if, which can be found in tr1 or boost.</p>
<p>derivation could be used as well, T-&gt;const T would then involve intentional slicing</p>
<pre><code class="language-cpp">template&lt;class T&gt; class MyPointer : public MyPointer&lt; const T &gt;
{
...
// hide any functions from base or overload with using, depending on whether constness is supposed to be transitive or not
};

template &lt;class T&gt; class MyPointer&lt; const T &gt;
{
...
};
</code></pre>
<pre><code class="language-cpp">template&lt;class T&gt; class MyPointer
{
...
// eat any MyPointer and a corresponding pointer to member
    template &lt;typename U, typename V&gt;
    MyPointer(const MyPointer&lt;U&gt;&amp; other, V U::* pm  )
        :  pointer( &amp;( *other ).*pm ), someAdditionalData( other.SomeAdditionalData() )
    {}
...
};

MyPointer&lt;ClassToPointTo const&gt; const p( GeneratePointer(), &amp;ClassToPointTo::firstMember );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855192</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855192</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 13 Feb 2010 21:35:09 GMT</pubDate></item><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Thu, 04 Mar 2010 21:52:15 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">template&lt;class T&gt; class MyPointer
{
...
// eat any MyPointer and a corresponding pointer to member
    template &lt;typename U, typename V&gt;
    MyPointer(const MyPointer&lt;U&gt;&amp; other, V U::* pm  )
        :  pointer( &amp;( *other ).*pm ), someAdditionalData( other.SomeAdditionalData() )
    {}
...
};

MyPointer&lt;ClassToPointTo const&gt; const p( GeneratePointer(), &amp;ClassToPointTo::firstMember );
</code></pre>
</blockquote>
<p>Last 2 weeks I trying to understand this but without any success.</p>
<p><code>&amp;ClassToPointTo::firstMember</code><br />
Such pointers are beyond my understanding.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1864549</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1864549</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 04 Mar 2010 21:52:15 GMT</pubDate></item><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Thu, 04 Mar 2010 22:10:37 GMT]]></title><description><![CDATA[<p>I think this should be an ordinary memberfunction pointer:<br />
<a href="http://www.newty.de/fpt/callback.html#member" rel="nofollow">http://www.newty.de/fpt/callback.html#member</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1864559</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1864559</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 04 Mar 2010 22:10:37 GMT</pubDate></item><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Thu, 04 Mar 2010 22:19:06 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<p>I think this should be an ordinary memberfunction pointer:<br />
<a href="http://www.newty.de/fpt/callback.html#member" rel="nofollow">http://www.newty.de/fpt/callback.html#member</a></p>
</blockquote>
<p>It could be a pointer to member function if V is a function type, but that would be pointless with regards to the purpose of that class. I was thinking of a pointer to an ordinary non-function member.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1864563</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1864563</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 04 Mar 2010 22:19:06 GMT</pubDate></item><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Fri, 05 Mar 2010 01:03:56 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>I need to convert pointer <code>p</code> from &quot; <code>ClassToPointTo const</code> &quot; type into one that will point to the <code>firstMember</code> of <code>*p</code> object *without losing <code>someAdditionalData</code> *. I have function <code>GeneratePointer()</code> which will make pointer having <code>someAdditionalData</code> , but I need to work only with pointers to <code>firstMember</code> s of the objects:</p>
<pre><code class="language-cpp">MyPointer&lt;int const&gt; const p( WhatINeedToWriteHere??? GeneratePointer() ??? );
</code></pre>
<p>I need fast and good-looking way of doing such conversion.</p>
</blockquote>
<p>I'm afraid <em>that</em> particular pig won't fly. At least not without a global map, which again is problematic and/or slow in multi-threaded environments.</p>
<p>If there were a &quot;good&quot; way (whether eays or not), I'm sure one of the smarter among the C++ community would already have found it, and integrated it into the boost::shared_ptr implementation. (As a way to get a shared_ptr from a raw-pointer, that shares ownership with other existing shared_ptrs.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1864573</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1864573</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 05 Mar 2010 01:03:56 GMT</pubDate></item><item><title><![CDATA[Reply to Good way of converting user-defined pointers on Fri, 05 Mar 2010 08:28:05 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;typename T&gt;
class pointer
{
  T* ptr;
public:
  explicit pointer(T* p=0) : ptr(p) {}

  T* get() const {return ptr;}
  T* operator-&gt;() const {return ptr;}
  T&amp; operator*() const {return *ptr;}

  template&lt;typename U&gt;
  pointer(pointer&lt;U&gt; const&amp; x,
    typename boost::enable_if&lt;
      boost::is_convertible&lt;U*,T*&gt;
    &gt;::type* =0)
  : ptr(x.get()) {}
};
</code></pre>
<p>(ungetestet)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1864613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1864613</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 05 Mar 2010 08:28:05 GMT</pubDate></item></channel></rss>