class Screen
{ public: typedef std::string::size_type index; char get() const; char get(index ht, index wd) const; private: std::string contents; index cursor; index height, width; };
成员指针定义为 string Screen::*,即成员指针既包括类地类型也报错成员的类型
string Screen::*ps_Screen = &Screen::contents初始化
Screen::index Screen::*pindex;
which says that pindex is a pointer to a member of classScreen with type Screen::index. We could assign the address of width to this pointer as follows:
pindex = &Screen::width;
成员函数的指针
char (Screen::*pmf)() const = &Screen::get;
char (Screen::*pmf2)(Screen::index, Screen::index) const; pmf2 = &Screen::get;
TYPEDEF
the following typedef defines Action to be an alternative name for the type of the two-parameter version ofget:
// Action is a type name typedef char (Screen::*Action)(Screen::index, Screen::index) const;
Action get = &Screen::get;
作为函数的参数
A pointer-to-member function type may be used to declare function parameters and function return types:
// action takes a reference to a Screen and a pointer to Screen member function Screen& action(Screen&, Action = &Screen::get);
This function is declared as taking two parameters: a reference to aScreen object and a pointer to a member function of class Screen taking twoindex parameters and returning a char. We could call action either by passing it a pointer or the address of an appropriate member function inScreen:
Screen myScreen; // equivalent calls: action(myScreen); // uses the default argument action(myScreen, get); // uses the variable get that we previously defined action(myScreen, &Screen::get); // pass address explicitly