使用方法 class 更改指向方法 class 的指针
changing a pointer to method class using method class
我想通过使用 class 方法更改指向 class 方法的指针的值,但它不起作用。这是代码:
测试代码class
class TestCode {
public:
TestCode() {};
virtual ~TestCode() {};
void render1() { (rendering code) }
void render2() { (rendering code) }
void event1(SDL_Event* e, void (TestCode::* point)()) {
if (e->key.keysym.sym == SDLK_t) { point = &TestCode::render2; }
}
};
主文件
int main(int argc, char* args[]) {
TestCode t1;
void (TestCode::* tptr1)(SDL_Event*, void (TestCode::*)()) = &TestCode::event1;
void (TestCode::* tptr2)() = &TestCode::render1;
while (!end) {
while (SDL_PollEvent(&ev)) {
if (ev.key.keysym.sym == SDLK_t) { tptr2 = &TestCode::render2; } //-> works
(t1.*tptr1)(&ev, tptr2); // -> doesnt work
}
(t1.*tptr2)();
}
return 0;
}
考虑到答案,我不得不通过引用传递指针。我已经更改了 event1 方法和 tptr1 指针。其余的是一样的。现在可以了。就像我说的,我还在学习 C++ 和 SDL。
事件 1
void event1(SDL_Event* e, void (TestCode::*& point)()) {
if (e->key.keysym.sym == SDLK_t) { point = &TestCode::render2; }
}
tptr1
void (TestCode::* tptr1)(SDL_Event*, void (TestCode::*&)()) = &TestCode::event1;
我想通过使用 class 方法更改指向 class 方法的指针的值,但它不起作用。这是代码:
测试代码class
class TestCode {
public:
TestCode() {};
virtual ~TestCode() {};
void render1() { (rendering code) }
void render2() { (rendering code) }
void event1(SDL_Event* e, void (TestCode::* point)()) {
if (e->key.keysym.sym == SDLK_t) { point = &TestCode::render2; }
}
};
主文件
int main(int argc, char* args[]) {
TestCode t1;
void (TestCode::* tptr1)(SDL_Event*, void (TestCode::*)()) = &TestCode::event1;
void (TestCode::* tptr2)() = &TestCode::render1;
while (!end) {
while (SDL_PollEvent(&ev)) {
if (ev.key.keysym.sym == SDLK_t) { tptr2 = &TestCode::render2; } //-> works
(t1.*tptr1)(&ev, tptr2); // -> doesnt work
}
(t1.*tptr2)();
}
return 0;
}
考虑到答案,我不得不通过引用传递指针。我已经更改了 event1 方法和 tptr1 指针。其余的是一样的。现在可以了。就像我说的,我还在学习 C++ 和 SDL。
事件 1
void event1(SDL_Event* e, void (TestCode::*& point)()) {
if (e->key.keysym.sym == SDLK_t) { point = &TestCode::render2; }
}
tptr1
void (TestCode::* tptr1)(SDL_Event*, void (TestCode::*&)()) = &TestCode::event1;