Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Does something like this fit the bill?</p> <pre><code>class W {}; class X {}; class Y {}; class Z : public X, public Y {}; template &lt;typename A, typename B&gt; class DualPointer { public: template &lt;typename T&gt; DualPointer(T *t) : a_ptr_(t), b_ptr_(t) {} operator A*() { return a_ptr_; } operator B*() { return b_ptr_; } private: A *a_ptr_; B *b_ptr_; }; int main() { Z z; DualPointer&lt;X, Y&gt; p(&amp;z); X *x = p; Y *y = p; return 0; } </code></pre> <p>Or if you're in a C++11 mood:</p> <pre><code>template &lt;typename... Ts&gt; class MultiPointer; template &lt;typename T, typename... Rest&gt; class MultiPointer&lt;T, Rest...&gt; : public MultiPointer&lt;Rest...&gt; { public: template &lt;typename U&gt; MultiPointer(U *u) : MultiPointer&lt;Rest...&gt;(u), ptr_(u) {}; operator T*() { return ptr_; } private: T *ptr_; }; template &lt;&gt; class MultiPointer&lt;&gt; { public: MultiPointer(void *) {} }; int main() { Z z; MultiPointer&lt;X, Y&gt; p(&amp;z); X *x = p; Y *y = p; return 0; } </code></pre> <p>If you're worried about the double storage of pointers, this approach doesn't work. See comments below this answer as to why incorporating Paweł's suggestion of using a union, which works if and only if all the pointers are numerically identical (that is, there's no multiple inheritance or other shenanigans using adjusted this pointers going on), is unsafe and basically useless.</p> <pre><code>// DON'T USE THIS VARIANT!!!! template &lt;typename... Ts&gt; class MultiPointer { public: MultiPointer(void *) {} }; template &lt;typename T, typename... Rest&gt; class MultiPointer&lt;T, Rest...&gt; { public: template &lt;typename U&gt; MultiPointer(U *u) : rest_(u) { ptr_ = u; }; template &lt;typename U&gt; operator U*() const { return rest_; } operator T*() const { return ptr_; } private: union { T *ptr_; MultiPointer&lt;Rest...&gt; rest_; }; }; </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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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