Pointer to a member function

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;
}
posted @ 2017-02-27 23:24  ch3cooh  阅读(102)  评论(0编辑  收藏  举报

Too young too simple. Sometimes naive! -- Quote from the elderly.