클래스 정의 내에서 포인터 멤버 함수를 어떻게 호출합니까? 내 코드 :
//Myclass.h
struct Booking{
int src;
int dest;
int pos;
};
class MyClass{
public:
void ExecutePlan();
private:
struct FlightPlan{
string name;
vector<Booking> bookings
};
typedef FlightPlan FP;
FP firstplan;
FP secondplan;
void FirstPlan(Booking& book);
void SecondPlan(Booking& book);
void Execute(FP& fplan, void (MyClass::*mptr)(Booking& book));
};
// Myclass.cpp
void MyClass::FirstPlan(Booking& book){
// do something with booking
}
void MyClass::SecondPlan(Booking& book){
// do something with booking
}
void MyClass::Execute(FP& fplan, void(MyClass::*mptr)(const FlightPlan& fp)){
for (int i=0; i<.fplan.bookings.size(); i++){
cout << "Executing Plan: "<< fplan.name << endl;
// Problematic line ...
mptr(bookings[i]); // <----- can't compile with this
}
}
void MyClass::Execute(){
// is this the correct design to call this member functions ???
Execute(firstplan, &MyClass::FirstPlan)
Execute(secondplan, &MyClass::SecondPlan)
}
멤버 함수를 포인터로 받도록 Execute Function을 어떻게 구성 할 수 있습니까?
Pls : C ++의 초보자입니다. 디자인이 이상 할 수도 있습니다!
폴
클래스 정의 내에서 포인터 멤버 함수를 호출하는 방법은 무엇입니까?
멤버 이름과 달리 멤버 포인터는에 암시 적으로 적용되지 않습니다 this
. 명시 적이어야합니다.
(this->*mptr)(fplan.bookings[i]);
이 멤버 함수를 호출하는 올바른 디자인입니다 ???
몇 가지 명백한 오류 (예 : ;
여기 저기 누락 , 정의에서 const FlightPlan&
의미 Booking&
하는 위치를 말함 Execute
)를 제외하고 나머지 코드는 괜찮아 보입니다. 구체적으로 특별히
Execute(firstplan, &MyClass::FirstPlan)
Execute(secondplan, &MyClass::SecondPlan)
멤버 함수 포인터를 얻기위한 올바른 구문입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다