#include <iostream>
namespace exercise {
class A {
public :
virtual void a( ) = 0 ;
virtual void b( ) = 0 ;
virtual void c( ) = 0 ;
} ;
class B : public A {
void a( ) { std:: cout << "a function @ B class" ; }
void b( ) { std:: cout << "b function @ B class" ; }
void c( ) { std:: cout << "c function @ B class" ; }
} ;
class C : public A {
void a( ) { std:: cout << "a function @ C class" ; }
void b( ) { std:: cout << "b function @ C class" ; }
void c( ) { std:: cout << "c function @ C class" ; }
} ;
using Pstd_mem = void ( A:: * ) ( ) ;
void foo( A* basePointer, Pstd_mem action) {
basePointer- > * action( ) ;
}
}
int main( void ) {
using namespace exercise;
A* b = new B( ) ;
A* c = new C( ) ;
foo( b, & A:: b ) ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKbmFtZXNwYWNlIGV4ZXJjaXNlIHsKCmNsYXNzIEEgewpwdWJsaWM6CgogICAgdmlydHVhbCB2b2lkIGEoKSA9IDA7CiAgICB2aXJ0dWFsIHZvaWQgYigpID0gMDsKICAgIHZpcnR1YWwgdm9pZCBjKCkgPSAwOwoKfTsKCmNsYXNzIEIgOiBwdWJsaWMgQSB7CgogICAgdm9pZCBhKCkgeyBzdGQ6OmNvdXQgPDwgImEgZnVuY3Rpb24gQCBCIGNsYXNzIjsgfQogICAgdm9pZCBiKCkgeyBzdGQ6OmNvdXQgPDwgImIgZnVuY3Rpb24gQCBCIGNsYXNzIjsgfQogICAgdm9pZCBjKCkgeyBzdGQ6OmNvdXQgPDwgImMgZnVuY3Rpb24gQCBCIGNsYXNzIjsgfQoKfTsKCmNsYXNzIEMgOiBwdWJsaWMgQSB7CgogICAgdm9pZCBhKCkgeyBzdGQ6OmNvdXQgPDwgImEgZnVuY3Rpb24gQCBDIGNsYXNzIjsgfQogICAgdm9pZCBiKCkgeyBzdGQ6OmNvdXQgPDwgImIgZnVuY3Rpb24gQCBDIGNsYXNzIjsgfQogICAgdm9pZCBjKCkgeyBzdGQ6OmNvdXQgPDwgImMgZnVuY3Rpb24gQCBDIGNsYXNzIjsgfQp9OwoKdXNpbmcgUHN0ZF9tZW0gPSB2b2lkKEE6OiopKCk7Cgp2b2lkIGZvbyhBKiBiYXNlUG9pbnRlciwgUHN0ZF9tZW0gYWN0aW9uKSB7CgogICAgYmFzZVBvaW50ZXItPiphY3Rpb24oKTsKCn0KfQoKaW50IG1haW4odm9pZCkgewoKdXNpbmcgbmFtZXNwYWNlIGV4ZXJjaXNlOwoKQSogYiA9IG5ldyBCKCk7CkEqIGMgPSBuZXcgQygpOwoKZm9vKGIsICZBOjpiKTsKCn0=
compilation info
prog.cpp: In function ‘void exercise::foo(exercise::A*, exercise::Pstd_mem)’:
prog.cpp:33:26: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘action (...)’, e.g. ‘(... ->* action) (...)’
basePointer->*action();
^
prog.cpp: In function ‘int main()’:
prog.cpp:43:4: warning: unused variable ‘c’ [-Wunused-variable]
A* c = new C();
^
stdout