   Why would we need pointer to members?
  I understand using pointer function is a good way to have callbacks. Similarly,I see that using pointer member function serves generally the same purpose. ButWhy do we need pointer to a class member ? For example consider the following :struct F{ int posX; F() : posX(0){}};typedef int Foo::*FooMemberPointerint main(){ FooMemberpointer ptrToX = &F::posX; F f; f.*ptrToX = 10; cout << f.*ptrToX << endl;}I am sure its the lack of experience thats causing me not to see where onewould generally use this. Thanks   
  The C++ standard doesn't differentiate between a pointer-to-member, and apointer-to-member function [5.3.1 - 2] except in that if the result of operator .* or ->* is a function then it can be called [5.5 - 6].In fact, the only way to avoid supporting pointer to member while supporting pointer to member function would have required significant changes to the standard to special case it for that single item. This is not something most language writers would do as it arbitrarily increases language complexity with little benefit.Furthermore, C++ was written with performance in mind, so it contains many constructs that, at the time, were considered optimization points for compiler vendors and software developers.lastly: it would be hard to implement any non-trivial non-invasive reflection system without pointer-to-member capabilities.   ;  Well, especially with all the operator overloading, why think differently over member functions and data members? A data member can overload the () operator, and behave like a function.I must admit that I haven't met a practical usage of this "feature".   ;Quote:Original post by DecriusWell, especially with all the operator overloading, why think differently over member functions and data members? A data member can overload the () operator, and behave like a function.I must admit that I haven't met a practical usage of this "feature".If you've used any decent amount of C++, then you've used that "feature" already.std::map is implemented in terms of std::less<T>, which is a functor (that's what objects that overload the function call operator are called) that returns the result of the comparison of two items.Functors are very powerful objects because they can maintain state and other important information that may be required to perform their operation. This is something that is hard to do in any portable way with straight functions, as static variables don't operate in the same manner.#include <vector>#include <iostream>#include <cmath>#include <iterator>#include <algorithm>struct add_val : std::unary_function<int, int> {add_val(int val) : amount(val) {}int operator()(int param) { return param + amount; }private:int amount;};int main() {std::vector<int> vals(5);std::generate(vals.begin(), vals.end(), rand);std::copy(vals.begin(), vals.end(), std::ostream_iterator<int>(std::cout, " "));std::endl(std::cout);std::transform(vals.begin(), vals.end(), vals.begin(), add_val(3));std::copy(vals.begin(), vals.end(), std::ostream_iterator<int>(std::cout, " "));std::endl(std::cout);std::transform(vals.begin(), vals.end(), vals.begin(), add_val(4));std::copy(vals.begin(), vals.end(), std::ostream_iterator<int>(std::cout, " "));std::endl(std::cout);std::sort(vals.begin(), vals.end(), std::greater<int>());std::copy(vals.begin(), vals.end(), std::ostream_iterator<int>(std::cout, " "));std::endl(std::cout);}