您的位置:首页 > 其它

Pointer to a member function

2017-02-27 23:24 316 查看

Why this post?

Because this looks like magic!

Code

// pointer to a member function
#include <iostream>

using namespace std;

class Dog
{
public:
void wag_tail() const
{
cout << "Tail left. Tail right. Wagging tail!" << endl;
}
void bark() const
{
cout << "Barrrrrrrrrrrk!" << endl;
}
};

int main()
{
Dog dog;
Dog * pdog = & dog;

void (Dog::*pfunc)() const = 0;
// point pfunc to wag_tail
pfunc = Dog::wag_tail;
(dog.*pfunc)();             // do NOT leave the first ()
(pdog->*pfunc)();           // do NOT leave the first ()
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: