* ->* 연산자 샘플

By | 2014년 5월 13일
#include "stdafx.h"

class CStateManager
{
private:
    /// 0: idle state
	/// 1: attack state
	/// 2: game over state

	int m_iState;

	void State_Idle();
	void State_Attack();
	void State_GameOver();

public:
	CStateManager();
	void SetState(int state);
	void DoIt();
}; //Class CStateManager

void CStateManager::State_Idle()
{
	printf("idle\n");
}

void CStateManager::State_Attack()
{
	printf("Attack\n");
}

void CStateManager::State_GameOver()
{
	printf("Game Over\n");
}

//생성자
CStateManager::CStateManager()
{
	m_iState =0;
}

void CStateManager::SetState(int state)
{
	m_iState = state;
}

void CStateManager::DoIt()
{
	//switch(m_iState)
	//{
	//case 0:
	//	State_Idle();
	//	break;
	//case 1:
	//	State_Attack();
	//	break;
	//case 2:
	//	State_GameOver();
	//	break;
	//}
	static void(CStateManager::*fp[3])()=
	{
		&CStateManager::State_Idle,
		&CStateManager::State_Attack,
		&CStateManager::State_GameOver
	};
	(this->*fp[m_iState])();
}

int main()
{	
	void (CStateManager::*fp)(int);  //pinter to a member function

	fp = &CStateManager::SetState;

	CStateManager sman;

	//sman.SetState(0);
	//sman.DoIt();

	(sman.*fp)(1);
	sman.DoIt();

	(sman.*fp)(2);
	sman.DoIt();
	
	char c;
	scanf("%c",&c);//stop

	return 0;
}
Category: C++

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다