> Wow. I never actually knew you could do this.
Especially after the advent of C++11, most programmers need never know about these esoteric beasts. Even in the very old days (pre-boost), the occasions to use them were rare. Now, with standard library provided polymorphic binder and polymorphic call wrapper, the usage of pointers to members in non-library C++ code had virtually disappeared.
> So what are the advantages of this over a regular pointer to a double?
The basic idea is straightforward. A pointer to an object can point to different objects of compatible types (or can be null). A pointer to member can point to different non-static members of compatible types (or can be null). Just a a pointer to object gives us the flexibility decide which object at run-time, a pointer to member gives us the flexibility decide which member at runtime. An example of this kind of usage (obsoleted by std::bind() and std::function<>) used to be in implementing flexible callbacks (say in an observer pattern).
Pointers to members are still useful for building abstractions (both std::bind() and std::function<> internally use pointers to member functions). For instance, in implementing the safe-bool idiom.
http://www.cplusplus.com/forum/general/104056/#msg561018
> These would be restricted to pointing to doubles only of that class?
> What if they didn't? Compile-time error?
Yes.
Though the object with which the pointer is dereferenced could be a derived class object. And if a pointer to a member function points to a virtual function in the base class, there are no surprises: it behaves polymorphically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
struct base
{
double length ;
double width ;
double height ;
virtual ~base() {}
void not_polymorphic() const { std::cout << "base::not_polymorphic\n" ; }
virtual void polymorphic() const { std::cout << "base::polymorphic\n" ; }
};
void foo( const base& b, void (base::*pmfn)() const ) { (b.*pmfn)() ; }
int main()
{
struct derived : base
{
void not_polymorphic() const /* hide */
{ std::cout << "derived::not_polymorphic (hides base::not_polymorphic)\n" ; }
virtual void polymorphic() const override
{ std::cout << "derived::polymorphic (overrides base::polymorphic)\n" ; }
};
base b ;
derived d ;
void (base::*pmfn)() const = &base::not_polymorphic ;
foo( b, pmfn ) ; // base::not_polymorphic
foo( d, pmfn ) ; // base::not_polymorphic
pmfn = &base::polymorphic ;
foo( b, pmfn ) ; // base::polymorphic
foo( d, pmfn ) ; // derived::polymorphic (overrides base::polymorphic)
}
|
http://coliru.stacked-crooked.com/a/77678f51c6021eec