Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code fails to compile for some obvious reasons. Most of the <code>typenameS</code> are unnecessary. I suppose <code>nullptr</code> is some homebrew <code>#define</code> (It is not the C++11 <code>nullptr</code> for sure).</p> <p>Here is a prettified, compiling version that makes it easier to see what is actually going on:</p> <pre><code>#include &lt;iostream&gt; template&lt;class C, class M&gt; inline C* get_parent_this(M C::*member_ptr, M* const member_this) { C* base = NULL; // !!! this is the tricky bit !!! char* member = reinterpret_cast&lt;char*&gt;(&amp;(base-&gt;*member_ptr)); return reinterpret_cast&lt;C*&gt;(reinterpret_cast&lt;char*&gt;(member_this) - member ); } class Owner { int x, pr_y; virtual int get_y() {return pr_y;} void set_y(int v) {pr_y = v;} public: struct { operator int() { return get_parent_this(&amp;Owner::y, this)-&gt;get_y(); } void operator =(int v) { get_parent_this(&amp;Owner::y, this)-&gt;set_y(v); } } y; }; </code></pre> <p>The tricky bit: This involves the dereference of a null-pointer. This is somewhat equal to the way the macro <code>offsetof</code> in <code>stddef.h</code> used to be (and still is) defined in some compilers. This works reliably on some compilers but is undefined behavior. Here are a few links to discussions about the topic:</p> <ul> <li><a href="https://stackoverflow.com/questions/7511211/address-of-null-pointer/7511337#7511337">Address of Null pointer?</a></li> <li><a href="http://en.wikipedia.org/wiki/Offsetof" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Offsetof</a></li> </ul> <p>I don't think it is worth repeating the discussion here. I really don't see what the code is buying you, besides obfuscated public data. For the few cases where you really need a <code>setter</code> I would just write it instead of using this. If your coding style prohibits public data for idiosyncratic reasons, write a set of macros to define them.</p>
 

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