title: Differences between using pointer to the class member and the structure member
body: <p>I am new to c++.I was told the structures and the classes are almost the same.The main differece is classes in c++ are by default private while structures are public.</p>
  
  <p>I have two different codes where we use pointers to structure members and classe members.</p>
  
  <pre><code>struct Simple { int a; };
  int main() {
  Simple so, *sp = &amp;so;
  sp-&gt;a;
  so.a;
  }
  </code></pre>
  
  <p>We use pointers to struct members in the above way.</p>
  
  <p>Why do we have to use the pointers to class members int he following way?</p>
  
  <pre><code>class X {
  public:
    int a;
    void f(int b) {
      cout &lt;&lt; "The value of b is "&lt;&lt; b &lt;&lt; endl;
    }
  };
  
  int main() {
  
    // declare pointer to data member
    int X::*ptiptr = &amp;X::a;
    int X::*ptiptr1 = &amp;X::a;
  
    // create an object of class type X
    X xobject,xobject1;
  
    // initialize data member
    xobject.*ptiptr = 10;
    xobject-&gt;*ptiptr1 = 11;
  
  }
  </code></pre>
  
  <p>Why are we first declaring a pointer to the class data member? Why can't we do it like we have done for structures?</p>
  
  <p>Please help me out.Thanks.</p>