成员函数指针

成员函数指针的行为含香函数指针,但你想调用他们时,除了参数之外,你还必须传递一个对象

class Parrot
{
public:
	void Eat()
	{
		cout << "Tsk, knick, tsk..." << endl;
	}

	void Speak()
	{
		cout << "On Captain, My Captain" << endl;
	}
};

int main()
{
	// Define a type: pointer to a member function of Parrot,taking no arguments and return void
	typedef void(Parrot::*TpMemFun) ();
        
        // Create an object of that type and initialize it  with the address of Parrot::Eat
	TpMemFun pActivity = &Parrot::Eat;
        // Create a Parrot...
	Parrot geronimo;
        // a pointer to it...
	Parrot *pGeronimo = &geronimo;
	
        // Invoke the member function stored in Activity
        // via an object. Notice the user of operator.*
	(geronimo.*pActivity)();

        // Same, via pointer. Now we use operator->*   
	(pGeronimo->*pActivity)();
	
        // Change the activity
	pActivity = &Parrot::Speak;
	(geronimo.*pActivity)();

	return 0;
}
你可以获取一般函数的 reference, 却无法获得成员函数的 reference。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值