Pointer to a class's private data member <p>Consider a class <code>card</code> which has two public members, <code>int suit</code> and <code>int value</code>, and a template function that sorts an array of cards by the member I pass through a pointer-to-member, like this:</p> <pre><code>//class card with public members class card{ public: int suit; int value; }; //sorting algorithm template&lt;typename m_pointer, typename iterator, typename Functype&gt; void sort_array(m_pointer member, iterator begin, iterator end, Functype pred){ iterator iter1=begin; while(iter1!=end &amp;&amp; ++iter1!=end){ iterator iter2=iter1; while(iter2!=begin){ iterator iter3=iter2; --iter3; //here i use the pointer-to-member to sort the cards if(pred((*iter3).*member, (*iter2).*member)){ std::swap(*iter3, *iter2); } else break; --iter2; } } } int main(){ card array[3]={{3,1},{2,3},{4,5}}; //let's sort the cards by the suit value in a decreasing order sort(&amp;card::suit, array, array+3, [](int a, int b){return a&lt;b;}); } </code></pre> <p>If the card member <code>suit</code> is public there's obviously no problem, but what actually i didn't expected is that the same code doesn't give any trouble even if i declare <code>suit</code> or <code>value</code> as private members.</p> <pre><code>class card{ int suit; int value; public://adding this for clarity, read forward int* pointer_to_suit(); }; </code></pre> <p>From what I know, I shouldn't be able to access private members from outside the class, and the only way to pass a pointer-to-member to a private member is through a member function which returns the member address, like this for example:</p> <pre><code>//function member of the class card int* card::pointer_to_suit(){ return &amp;suit; } </code></pre> <p>So, why is it possible that the code above (the one with the template) works?</p> <p><strong>EDIT:</strong> Ok, the code above doesn't compile on it's own, but for some reason the following code compile fine to me. I'll post the whole code since I've no idea where the trick for it to work might be, sorry for the mess:</p> <pre><code>template&lt;typename m_pointer, typename iterator, typename Functype&gt; void sort_array(m_pointer member, iterator begin, iterator end, Functype pred){ iterator iter1=begin; while(iter1!=end &amp;&amp; ++iter1!=end){ iterator iter2=iter1; while(iter2!=begin){ iterator iter3=iter2; --iter3; if(pred((*iter3).*puntatore, (*iter2).*puntatore)){ std::swap(*iter3, *iter2); } else break; --iter2; } } } class card{ int suit; int value; public: card(): suit(0), value(0) {} card(int a, int b): suit(a), value(b){} bool operator==(card a){return (suit==a.get_s() &amp;&amp; value==a.get_v());} bool operator!= (card a){return !(*this==a);} void set_values(int a, int b){suit=a; value=b;} int get_v(){return value;} void set_v(int v){value=v;} int get_s(){return suit;} void set_s(int s){suit=s;} double points_card(); }; template&lt;typename iterator&gt; void ordina(iterator begin, iterator end, short (&amp;suit)[4]){ for(int i=0; i&lt;4; i++) suit[i]=0; iterator it1=begin; while(it1!=end){ if((*it1).get_s()==1) suit[0]+=1; else if((*it1).get_s()==2) suit[1]+=1; else if((*it1).get_s()==3) suit[2]+=1; else if((*it1).get_s()==4) suit[3]+=1; ++it1; } sort_array(&amp;carte::suit, begin, end, [](char a, char b){ if(b==0) return false; else if(a==0) return true; return (a&gt;b); }); sort_array(&amp;carte::value, begin, begin+suit[0], [](int a, int b){return (a&lt;b);}); sort_array(&amp;carte::value, begin+suit[0], begin+suit[0]+suit[1], [](int a, int b){return (a&lt;b);}); sort_array(&amp;carte::value, begin+suit[0]+suit[1], begin+suit[0]+suit[1]+suit[2], [](int a, int b){return (a&lt;b);}); sort_array(&amp;carte::value, begin+suit[0]+suit[1]+suit[2], begin+suit[0]+suit[1]+suit[2]+suit[3],[](int a, int b){return (a&lt;b);}); } int main(){ card array[5]={{2,3},{1,2},{3,4},{4,5},{3,2}}; short suits[4]={1,1,2,1}; ordina(array, array+5, suits); return 0; } </code></pre> <p><strong>EDIT 2:</strong> Yes, it runs <a href="http://coliru.stacked-crooked.com/a/d1795f0845770fcb" rel="nofollow">http://coliru.stacked-crooked.com/a/d1795f0845770fcb</a> . Please note that the code here is not translated and there are some lines i didn't add for brevity.</p> <p><strong>EDIT 3:</strong> As mentioned in Barry answer <a href="http://stackoverflow.com/a/35978073/5922196">http://stackoverflow.com/a/35978073/5922196</a>, this is a <code>gcc</code> compiler bug. I used <code>g++ 4.9.2</code> and this bug is still unresolved</p>