나는 엔진을 만들고있다. 별도의 클래스에서 포인터로 함수를 호출하는 Timer
클래스를 생성해야합니다. 예를 들어 :포인터를 사용하여 클래스에서 함수를 호출하는 방법은 무엇입니까?
class MyTimer {
public:
void setTimeoutFunction(_pointer_, unsigned short timeoutMs) {
// here we need to have a opportunity to store a _pointer_ to a function
}
void tickTimer() {
...
// here I need to call a function by a pointer
...
}
};
// Main class:
class MyAnyClass {
public:
void start() {
MyTimer myTimer;
myTimer.setTimeoutFunction(startThisFunc, 1500); // 1500ms = 1.5s
while (true) {
myTimer.tickTimer();
}
}
void startThisFunc() { ... }
}
, 어떻게 당신은 어떤 클래스에 속하는 함수에 대한 포인터를 저장 않으며 포인터에 의해 그 함수를 호출?
이 질문에 좋은 답변을 얻을 너무 광범위하지만 어쩌면 원하는 것을 보관할 수있는 [std :: function] (http://en.cppreference.com/w/cpp/utility/functional/function)을 살펴보아야합니다. – mkaes
한 번에 하나의 개체에 대해 하나의 전용 타이머가 항상 있어야합니까? 아니면 단일 타이머 개체가 다른 형식의 여러 개체를 처리 할 수 있어야합니까? –
@KerrekSB 한 번에 한 개체에 하나의 타이머가 필요합니다. – JavaRunner