类的成员指针(对比于c语言中的函数指针)

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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值