[ create a new paste ] login | about

Link: http://codepad.org/xhsizSDh    [ raw code | output | fork ]

C++, pasted on Feb 9:
#include <iostream>

class Hoge
{
private:
	class Lock
	{
	public:
		Lock(Hoge *owner) : m_owner(owner)
		{
			m_owner->methodAorB = &Hoge::methodB;
		}
		~Lock(void)
		{
			m_owner->methodAorB = &Hoge::methodA;
		}
	private:
		Hoge *m_owner;
	};
public:
	Hoge(void) : methodAorB(&Hoge::methodA)
	{
	}
	void method(void)
	{
		(this->*methodAorB)();
	}
	void call_method(void)
	{
		Lock lock(this);

		method();
	}
private:
	void methodA(void)
	{
		std::cout << "methodA" << std::endl;
	}
	void methodB(void)
	{
		std::cout << "methodB" << std::endl;
	}
	void (Hoge::*methodAorB)(void);
};

int main(void)
{
	Hoge h;

	h.method();
	h.method();
	h.method();

	h.call_method();
	h.call_method();
	h.call_method();

	h.method();
	h.method();
	h.method();

	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
methodA
methodA
methodA
methodB
methodB
methodB
methodA
methodA
methodA


Create a new paste based on this one


Comments: