일부 레거시 C++ 코드를 리팩터링하는 동안 동일한 서명을 공유하는 모든 클래스 메서드를 가리킬 수있는 변수를 어떻게 든 정의하여 일부 코드 중복을 제거 할 수 있음을 발견했습니다. - 는이 가야 할 올바른 방법입니다C++ 클래스 메서드에 대한 포인터
class MyClass
{
protected:
bool CaseMethod1(int abc, const std::string& str)
{
cout << "case 1:" << str;
return true;
}
bool CaseMethod2(int abc, const std::string& str)
{
cout << "case 2:" << str;
return true;
}
bool CaseMethod3(int abc, const std::string& str)
{
cout << "case 3:" << str;
return true;
}
public:
bool TestSwitch(int num)
{
bool (MyClass::*CaseMethod)(int, const std::string&);
switch (num)
{
case 1: CaseMethod = &MyClass::CaseMethod1;
break;
case 2: CaseMethod = &MyClass::CaseMethod2;
break;
case 3: CaseMethod = &MyClass::CaseMethod3;
break;
}
...
bool res = CaseMethod(999, "hello world");
...
reurn res;
}
};
내 질문은 : 조금 파고 후, 나는 다음과 같이 뭔가를 할 수 있다는 것을 발견? 무엇이든을 고려해야합니까 부스트가 제공 하는가?
편집 ...
이좋아, 내 실수 - 지금처럼 내가 호출하는 메소드 :
bool res = ((*this).*CaseMethod)(999, "Hello World");