이러한 문제를 해결해야합니다. 기본 클래스와 두 개의 상속 된 클래스가 있습니다. 기본 클래스는 함수 포인터를 매개 변수로 필요로하는 메서드를 포함합니다. 그러나 이러한 함수는 상속 된 클래스에서 정의됩니다.멤버 함수 포인터 및 상속
오류 C2664 : 'CBase ::는 waitEvent'
class CBase; typedef bool (CBase::*FPredicate)(); class CBase { public: CBase() {} ~CBase() {} protected: //this method waits until 'predicate' is true or until 'timeout' ms. passed //and returns true if 'predicate' is true eventually bool WaitEvent(FPredicate predicate, int timeout) { bool result = false; int time1 = GetTickCount(); int time2; bool isEnd = false; while(!isEnd) { result = isEnd = (this->*predicate)(); time2 = GetTickCount(); if(time2 - time1 > timeout && !isEnd) isEnd = true; } return result; } }; class CChildA : public CBase { protected: bool a1() {/*some work*/} bool a2() {/*some work*/} void a_main() { ... WaitEvent(&CChildA::a1, 100); ... WaitEvent(&CChildA::a2, 100); ... } }; class CChildB : public CBase { protected: bool b1() {/*some work*/} bool b2() {/*some work*/} void b_main() { ... WaitEvent(&CChildB::b1, 100); ... WaitEvent(&CChildB::b2, 100); ... } };
MSVC 2005 컴파일러는 waitEvent에 오류가 호출을 제공 부울 '에서 매개 변수 1 변환 할 수 없습니다 (__thiscall CChildA :: *) (무효) 'to'FPredicate '
질문 : 어떻게 작동 시키려면 코드를 변경해야합니까? WaitEvent 호출을 WaitEvent((FPredicate)(&CChildA::a1), 100)
으로 다시 쓰는 것이 안전할까요?
이 경우 컴파일러는 오류는 표시하지 않지만 안전합니까? 아니면 문제를 푸는 더 좋은 방법이 있습니까?
미리 감사드립니다.
부스트 또는 std :: tr1을 사용할 수 있습니까? 이 경우 함수을 사용하고 파생 클래스에서는 member 함수 –
stijn
@stijn을 사용하여 bind()를 사용합니다. C++ 11은 라이브이고'bind()'는 이미'std ::'에 있어야합니다. . 일상적인 프로그래밍에 도움이되는 몇 가지 작은 기능이 이미 사용할 수 있으므로 일반적으로 -std = C++ 0x를 사용하는 것이 좋습니다. – Griwes
파생 클래스에서 기본 클래스의 가상 함수에서 호출 할 수있는 함수를 만들 수 있습니까? –