Note that there are some explanatory texts on larger screens.

plurals
  1. POData member offset in C++
    primarykey
    data
    text
    <p>"<em>Inside the C++ Object Model</em>" says that the offset of a data member in a class is always 1 more than the actual offset in order to distinguish between the pointer to 0 and the pointer to the first data member, here is the example:</p> <pre><code>class Point3d { public: virtual ~Point3d(); public: static Point3d origin; float x, y, z; }; //to be used after, ignore it for the first question int main(void) { /*cout &lt;&lt; "&amp;Point3d::x = " &lt;&lt; &amp;Point3d::x &lt;&lt; endl; cout &lt;&lt; "&amp;Point3d::y = " &lt;&lt; &amp;Point3d::y &lt;&lt; endl; cout &lt;&lt; "&amp;Point3d::z = " &lt;&lt; &amp;Point3d::z &lt;&lt; endl;*/ printf("&amp;Point3d::x = %p\n", &amp;Point3d::x); printf("&amp;Point3d::y = %p\n", &amp;Point3d::y); printf("&amp;Point3d::z = %p\n", &amp;Point3d::z); getchar(); } </code></pre> <p>So in order to distinguish the two pointers below, the offset of a data member is always 1 more.</p> <pre><code>float Point3d::*p1 = 0; float Point3d::*p2 = &amp;Point3d::x; </code></pre> <p>The main function above is attempt to get the offset of the members to verify this argument, which is supposed to output: 5, 9, 13(Consider the vptr of 4bytes at the beginning). In <strong>MS Visual Studio 2012</strong> however, the output is:</p> <pre><code>&amp;Point3d::x = 00000004 &amp;Point3d::y = 00000008 &amp;Point3d::z = 0000000C </code></pre> <p><strong>Question: So is MS C++ compiler did some optimization or something to prevent this mechanism?</strong></p>
    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.
 

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