title: Objects classes and Member Function
body: <p>Executed with Online GDB Compiler.</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class test {
  public:
    int x = 5;
    double z = 5.89999;
    float y = 6.7;
  
    void print(){
      int m;
      cout &lt;&lt; &quot;Print Method&quot; &lt;&lt; endl;
    }
  };
  
  int main(){
    test t;
  
    cout &lt;&lt; *(int*)((char*)&amp;t + offsetof(test, x)) &lt;&lt; endl;
    cout &lt;&lt; *(float*)((char*)&amp;t + offsetof(test, y)) &lt;&lt; endl;
    cout &lt;&lt; *(double*)((char*)&amp;t + offsetof(test, z)) &lt;&lt; endl;
  
    //Accessing the pointer to the member function
  
    void (test::*mf)() = &amp;test::print;
  
    //Can we do the above statement using pointer arithmetic
  
    //Making a call to it
  
    (t.*mf)();
  
    //Can we do the above statement using pointer arithmetic?
  }
  </code></pre>
  <p>Output:</p>
  <pre class="lang-none prettyprint-override"><code>5
  6.7
  5.89999
  Print Method
  </code></pre>
  <p>I might have a lot of mistakes in my understanding, so kindly excuse me.</p>
  <p>A function name points to the starting address of the code segment of its own function.</p>
  <ol>
  <li><p>I created a pointer to the member function <code>void (test::*mf)()</code> and it is assigned with the pointer of the member function <code>print</code>. How can I access the member function's address using the class name? Where does the class name reside in memory, and how is it associated to the member function with respect to the address and memory space?</p>
  </li>
  <li><p>Memory space gets allocated for the member fields once the object is created, and objects don't have the pointer to the code segment of the member functions. So, a pointer to a member function is not part of the object, but can be invoked by the object. How does the object get a hold of that member function's pointer, and where is it hiding?</p>
  </li>
  <li><p><code>void (test::*mf)() = &amp;test::print;</code></p>
  <p>Can I do the above statement using pointer arithmetic?</p>
  <p><code>(t.*mf)();</code></p>
  <p>Can I do the above statement using pointer arithmetic?</p>
  </li>
  </ol>