Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While you could write a pointer wrapper class (similar to "smart pointers", but not so smart in this case) which only accepts pointers to types derived from <code>A</code> and <code>B</code>, <strong>dereferencing them becomes ambiguous</strong>. This is a <em>conceptual</em> problem you can't solve with <em>any</em> method.</p> <p>But you could provide two functions <code>toA()</code> and <code>toB()</code> and/or conversion operators in order to retrieve a pointer to one of the base classes. But as said, you can't (nicely) overload <code>operator*</code> to retrieve the base reference depending on the context (depending on whether an <code>A*</code> or a <code>B*</code> is needed in the context). Same with <code>operator-&gt;</code>.</p> <pre><code>template&lt;typename A, typename B&gt; class DualPointer { A *a; // We need two separate pointers because their distance is not known B *b; public: template&lt;typename T&gt; DualPointer(T* object) : a(object), b(object) { } A *toA() const { return a; } B *toB() const { return b; } operator A* () const { return a; } operator B* () const { return b; } }; </code></pre> <p>Using SFINAE you can also allow a function template which has the actual type as the template parameter, like <code>to&lt;MyBaseA&gt;()</code>:</p> <pre><code>template&lt;typename T&gt; typename std::enable_if&lt;std::is_same&lt;A, T&gt;::value, T*&gt;::type to() { return a; } template&lt;typename T&gt; typename std::enable_if&lt;std::is_same&lt;B, T&gt;::value, T*&gt;::type to() { return b; } </code></pre> <p><a href="http://ideone.com/93vnpg" rel="nofollow">Demonstration of this wrapper class</a></p> <p>You can then add such a pointer as a class member as requested in your question:</p> <pre><code>class MyClass { DualPointer&lt;MyBaseA, MyBaseB&gt; pointer; }; </code></pre> <p>and access the pointer like:</p> <pre><code>pointer.toA()-&gt;memberFunctionOfA(); </code></pre> <p>If your types <code>A</code> and <code>B</code> are fixed, then either drop the "template" line and replace <code>A</code> and <code>B</code> accordingly, or add a <code>typedef DualPointer&lt;MyBaseA, MyBaseB&gt; MyAOrB;</code></p> <hr> <p>Expanding on this, you could say <strong>one of the two base classes</strong>, say the first, is your "main" base class. That could then be the one the pointer acts <em>like</em>, so the one returned by <code>operator*</code> and <code>operator-&gt;</code>. The two operators would then look like:</p> <pre><code>A * operator-&gt; () const { return a; } A &amp; operator* () const { return *a; } </code></pre> <p>Then the call from above can become as easy as</p> <pre><code>pointer-&gt;memberFunctionOfA(); </code></pre> <p><strong>but not simultaneously</strong></p> <pre><code>pointer-&gt;memberFunctionOfB(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload